Table of Contents

Has many

The information below is copied from the API documentation on has_many associations
http://api.akelos.org/ActiveRecord/Associations/AkHasMany.html

$has_many adds the following methods for retrieval and query of collections of associated objects.

'collection' is replaced with the singular form of current association, so var $has_many = 'clients' would hold an array of objects on $this→clients and a collection handling interface instance on $this→client (singular form)

Example

A Firm class declares has_many clients, which will add:

$Firm->client->load() (similar to $Clients->find('all', array('conditions' => 'firm_id = '.$id)) )
$Firm->client->add()
$Firm->client->delete()
$Firm->client->assign()
$Firm->client->assignByIds()
$Firm->client->clear()
$Firm->client->isEmpty() (similar to count($Firm->clients) == 0)
$Firm->client->getSize() (similar to Client.count "firm_id = #{id}")
$Firm->client->find() (similar to $Client->find($id, array('conditions' => 'firm_id = '.$id)) )
$Firm->client->build() (similar to new Client(array('firm_id' => $id)) )
$Firm->client->create() (similar to $c = new Client(array('firm_id' => $id)); $c->save(); return $c )

The declaration can also include an options array to specialize the behavior of the association.

Options

Option examples

$has_many = array(
            'comments'  => array('order' => 'posted_on', 'include' => 'author', 'dependent' => 'nullify'),
            'people'    => array('conditions' => 'deleted = 0', 'order' => 'name'),
            'tracks'    => array('order' => 'position', 'dependent' => 'destroy'),
            'members'   => array('class_name' => 'Person', 'conditions' => 'role = "member"'));