[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActiveRecord/AkAssociations/ -> AkBelongsTo.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: 287 lines (13 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 1 file
 AkActiveRecord/AkAssociation.php

Defines 1 class

AkBelongsTo:: (12 methods):
  addAssociated()
  getType()
  findAssociated()
  assign()
  build()
  create()
  load()
  replace()
  getAssociatedFinderSqlOptions()
  constructSqlForInclusion()
  beforeSave()
  beforeDestroy()


Class: AkBelongsTo  - X-Ref

Adds the following methods for retrieval and query for a single associated object that this object holds an id to.
* <tt>belongsTo->assign($association_id, $Associate);</tt> - assigns the associate object, extracts the primary key, and sets it as the foreign key.
* <tt>belongsTo->build($association_id, $attributes = array())</tt> - returns a new object of the associated type that has been instantiated
with +attributes+ and linked to this object through a foreign key but has not yet been saved.
* <tt>belongsTo->create($association_id, $attributes = array())</tt> - returns a new object of the associated type that has been instantiated
with +attributes+ and linked to this object through a foreign key and that has already been saved (if it passed the validation).

Example: A Post class declares <tt>belongsTo('author')</tt>, which will add:
* <tt>$Post->author->load()</tt> (similar to <tt>$Author->find($author_id)</tt>)
* <tt>$Post->author->assign($Author)</tt> (similar to <tt>$Post->author_id = $Author->getId();</tt>)
* <tt>$Post->author->build($Author);</tt> (similar to <tt>$Post->author = new Author();</tt>)
* <tt>$Post->author->create($Author);</tt> (similar to <tt>$Post->author = new Author(); $Post->author->save();</tt>)
The declaration can also 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>belongsTo('author')</tt> will by default be linked to the 'Author' class, but
if the real class name is 'Person', you'll have to specify it with this option.
* <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 from which the associated object will be picked at the top. Specified as
an "ORDER BY" sql fragment, such as "last_name, first_name DESC"
* <tt>primary_key_name</tt> - specify the 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 a 'Person' class that makes a belongsTo association to a
'Boss' class will use "boss_id" as the default primary_key_name.
* <tt>counter_cache</tt> - caches the number of belonging objects on the associate class through use of increment_counter
and decrement_counter. The counter cache is incremented when an object of this class is created and decremented when it's
destroyed. This requires that a column named "#{table_name}_count" (such as comments_count for a belonging Comment class)
is used on the associate class (such as a Post class).

Option examples:
belongsTo('firm', array('primary_key_name' => 'client_of'));
belongsTo('author', array('class_name' => 'Person', 'primary_key_name' => 'author_id'));
belongsTo('valid_coupon', array('class_name' => 'Coupon', 'primary_key_name' => 'coupon_id', 'conditions' => "'discounts' > 'payments_count'"));
addAssociated($association_id, $options = array()   X-Ref
No description

getType()   X-Ref
No description

findAssociated($association_id)   X-Ref
No description

assign($association_id, &$Associated)   X-Ref
No description

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

create($association_id, $attributes = array()   X-Ref
Returns a new object of the associated type that has been instantiated with attributes
and linked to this object through a foreign key and that has already been saved (if it passed the validation)


load($association_id)   X-Ref
No description

replace($association_id, &$NewAssociated)   X-Ref
No description

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

constructSqlForInclusion($association_id)   X-Ref
No description

beforeSave(&$object)   X-Ref
Triggers


beforeDestroy(&$object)   X-Ref
No description



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