Class: AkHasOne - X-Ref
Adds the following methods for retrieval and query of a single associated object.
$association is replaced with the symbol passed as the first argument, so
<tt>hasOne('manager')</tt> would add among others <tt>$this->manager->getAttributes()</tt>.
Example: An Account class declares <tt>hasOne('beneficiary');</tt>, which will add:
* <tt>$Account->beneficiary->load()</tt> (similar to <tt>$Beneficiary->find('first', array('conditions' => "account_id = $id"))</tt>)
* <tt>$Account->beneficiary->assign($Beneficiary);</tt> (similar to <tt>$Beneficiary->account_id = $Account->id; $Beneficiary->save()</tt>)
* <tt>$Account->beneficiary->build();</tt> (similar to <tt>$Beneficiary = new Beneficiary("account_id->", $Account->id)</tt>)
* <tt>$Account->beneficiary->create();</tt> (similar to <tt>$b = new Beneficiary("account_id->", $Account->id); $b->save(); $b</tt>)
The declaration can also include an options array 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>hasOne('manager')</tt> will by default be linked to the "Manager" 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 "rank = 5".
* <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>dependent</tt> - if set to true, the associated object is destroyed when this object is. It's also destroyed if another
association is assigned.
* <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 hasOne association will use "person_id"
as the default foreign_key.
Option examples:
var $hasOne = array(
'credit_card' => array('dependent' => true),
'last_comment' => array('class_name' => "Comment", 'order' => "posted_on"),
'project_manager' => array('class_name' => "Person", 'conditions' => "role = 'project_manager'")
);
|
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)
|