Class: AkHasAndBelongsToMany - X-Ref
Associates two classes via an intermediate join table. Unless the join table is explicitly specified as
an option, it is guessed using the lexical order of the class names. So a join between Developer and Project
will give the default join table name of "developers_projects" because "D" outranks "P".
Adds the following methods for retrieval and query.
'collection' is replaced with the associton identification passed as the first argument, so
<tt>var $has_and_belongs_to_many = 'categories'</tt> would make available on the its parent an array of
objects on $this->categories and a collection handling interface instance on $this->category (singular form)
* <tt>collection->load($force_reload = false)</tt> - returns an array of all the associated objects.
An empty array is returned if none is found.
* <tt>collection->add($object, ...)</tt> - adds one or more objects to the collection by creating associations in the join table
(collection->push and $collection->concat are aliases to this method).
* <tt>collection->pushWithAttributes($object, $join_attributes)</tt> - adds one to the collection by creating an association in the join table that
also holds the attributes from <tt>join_attributes</tt> (should be an array with the column names as keys). This can be used to have additional
attributes on the join, which will be injected into the associated objects when they are retrieved through the collection.
(collection->concatWithAttributes() is an alias to this method).
* <tt>collection->delete($object, ...)</tt> - removes one or more objects from the collection by removing their associations from the join table.
This does not destroy the objects.
* <tt>collection->set($objects)</tt> - replaces the collections content by deleting and adding objects as appropriate.
* <tt>collection->setByIds($ids)</tt> - replace the collection by the objects identified by the primary keys in $ids
* <tt>collection->clear()</tt> - removes every object from the collection. This does not destroy the objects.
* <tt>collection->isEmpty()</tt> - returns true if there are no associated objects.
* <tt>collection->size()</tt> - returns the number of associated objects. (collection->count() is an alias to this method)
* <tt>collection->find($id)</tt> - finds an associated object responding to the +id+ and that
meets the condition that it has to be associated with this object.
Example: A Developer class declares <tt>var $has_and_belongs_to_many = 'projects'</tt>, which will add:
* <tt>Developer->projects</tt> (The collection)
* <tt>Developer->project</tt> (The association manager)
* <tt>Developer->project->add()<<</tt>
* <tt>Developer->project->pushWithAttributes()</tt>
* <tt>Developer->project->delete()</tt>
* <tt>Developer->project->set()</tt>
* <tt>Developer->project->setByIds()</tt>
* <tt>Developer->project->clear()</tt>
* <tt>Developer->projects->isEmpty()</tt>
* <tt>Developer->projects->size()</tt>
* <tt>Developer->projects->find($id)</tt>
The declaration may include an options hash to specialize the behavior of the association.
Options are:
* <tt>class_name</tt> - specify the class name of the association. Use it only if that name can't be inferred
from the association name. So <tt>var $has_and_belongs_to_many = 'projects'</tt> will by default be linked to the
Project class, but if the real class name is SuperProject, you'll have to specify it with this option.
* <tt>table_name</tt> - Name for the associated object database table. As this association will not instantiate the associated model
it nees to know the name of the table we are going to join. This is infered form previous class_name
* <tt>join_table</tt> - specify the name of the join table if the default based on lexical order isn't what you want.
WARNING: If you're overwriting the table name of either class, the table_name method MUST be declared underneath any
$has_and_belongs_to_many declaration in order to work.
* <tt>join_class_name</tt> - specify the class name of the association join table. If the class does not exist, a new class
will be created on runtime to load the results into the collection. The class name will be infered from the join_table name
* <tt>foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
of this class in lower-case and "_id" suffixed. So a Person class that makes a $has_and_belongs_to_many association
will use "person_id" as the default foreign_key.
* <tt>association_foreign_key</tt> - specify the association foreign key used for the association. By default this is
guessed to be the name of the associated class in lower-case and "_id" suffixed. So if the associated class is Project,
the has_and_belongs_to_many association will use "project_id" as the default association foreign_key.
* <tt>conditions</tt> - specify the conditions that the associated object must meet in order to be included as a "WHERE"
sql fragment, such as "authorized = 1".
* <tt>order</tt> - specify the order in which the associated objects are returned as a "ORDER BY" sql fragment, such as "last_name, first_name DESC"
* <tt>finder_sql</tt> - overwrite the default generated SQL used to fetch the association with a manual one
* <tt>delete_sql</tt> - overwrite the default generated SQL used to remove links between the associated
classes with a manual one
* <tt>insert_sql</tt> - overwrite the default generated SQL used to add links between the associated classes
with a manual one
* <tt>include</tt> - specify second-order associations that should be eager loaded when the collection is loaded.
* <tt>group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
* <tt>limit</tt>: An integer determining the limit on the number of rows that should be returned.
* <tt>offset</tt>: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
* <tt>select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
include the joined columns.
* <tt>unique</tt> - if set to true, duplicates will be omitted from the collection.
Option examples:
$has_and_belongs_to_many = 'projects';
$has_and_belongs_to_many = array('projects'=> array('include' => array('milestones', 'manager')))
$has_and_belongs_to_many = array('nations' => array('class_name' => "Country"))
$has_and_belongs_to_many = array('categories' => array('join_table' => "prods_cats"))
$has_and_belongs_to_many = array('active_projects' => array('join_table' => 'developers_projects', 'delete_sql' =>
'DELETE FROM developers_projects WHERE active=1 AND developer_id = $id AND project_id = $record->id'
|
add(&$Associated)
X-Ref
|
add($object), add(array($object, $object2)) - adds one or more objects to the collection by setting
their foreign keys to the collection?s primary key. Items are saved automatically when parent has been saved.
|