[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActiveRecord/AkAssociations/ -> AkHasAndBelongsToMany.php (summary)

(no description)

Author: Bermi Ferrer
Copyright: Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
License: GNU Lesser General Public License
File Size: 909 lines (39 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 2 files
 AkActiveRecord/AkAssociation.php
 AkInstaller.php

Defines 1 class

AkHasAndBelongsToMany:: (46 methods):
  addAssociated()
  getType()
  _setCollectionHandler()
  _loadJoinObject()
  _isJoinObjectLoaded()
  _loadJoinClass()
  _createJoinClass()
  _tableExists()
  _createJoinTable()
  load()
  add()
  push()
  concat()
  deleteAll()
  reset()
  set()
  setIds()
  setByIds()
  addId()
  delete()
  removeFromCollection()
  _setAssociatedMemberId()
  _unsetAssociatedMemberId()
  _getAssociatedMemberId()
  _hasAssociatedMember()
  _relateAssociatedWithOwner()
  _build()
  constructSql()
  associationJoin()
  count()
  build()
  create()
  getAssociatedFinderSqlOptions()
  constructSqlForInclusion()
  _hasCachedCounter()
  _getCachedCounterAttributeName()
  getAssociatedModelInstance()
  find()
  isEmpty()
  getSize()
  clear()
  afterCreate()
  afterUpdate()
  beforeDestroy()
  _afterCallback()
  _removeDuplicates()


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'
addAssociated($association_id, $options = array()   X-Ref
No description

getType()   X-Ref
No description

_setCollectionHandler($association_id, $handler_name)   X-Ref
No description

_loadJoinObject()   X-Ref
No description

_isJoinObjectLoaded()   X-Ref
No description

_loadJoinClass($class_name)   X-Ref
No description

_createJoinClass()   X-Ref
No description

_tableExists($table_name)   X-Ref
No description

_createJoinTable()   X-Ref
No description

load($force_reload = false)   X-Ref
No description

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.


push(&$record)   X-Ref
No description

concat(&$record)   X-Ref
No description

deleteAll()   X-Ref
Remove all records from this association


reset()   X-Ref
No description

set(&$objects)   X-Ref
No description

setIds()   X-Ref
No description

setByIds()   X-Ref
No description

addId($id)   X-Ref
No description

delete(&$Associated)   X-Ref
No description

removeFromCollection(&$records)   X-Ref
Remove records from the collection. Use delete() in order to trigger database dependencies


_setAssociatedMemberId(&$Member)   X-Ref
No description

_unsetAssociatedMemberId(&$Member)   X-Ref
No description

_getAssociatedMemberId(&$Member)   X-Ref
No description

_hasAssociatedMember(&$Member)   X-Ref
No description

_relateAssociatedWithOwner(&$Associated)   X-Ref
No description

_build($association_id, &$AssociatedObject, $reference_associated = true)   X-Ref
No description

constructSql()   X-Ref
No description

associationJoin()   X-Ref
No description

count()   X-Ref
No description

build($attributes = array()   X-Ref
No description

create($attributes = array()   X-Ref
No description

getAssociatedFinderSqlOptions($association_id, $options = array()   X-Ref
No description

constructSqlForInclusion()   X-Ref


_hasCachedCounter()   X-Ref
No description

_getCachedCounterAttributeName()   X-Ref
No description

getAssociatedModelInstance()   X-Ref
No description

find()   X-Ref
No description

isEmpty()   X-Ref
No description

getSize()   X-Ref
No description

clear()   X-Ref
No description

afterCreate(&$object)   X-Ref
Triggers


afterUpdate(&$object)   X-Ref
No description

beforeDestroy(&$object)   X-Ref
No description

_afterCallback(&$object)   X-Ref
No description

_removeDuplicates(&$object, $association_id)   X-Ref
No description



Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6