Containable Behavior
This model behavior allows you to filter and limit model
find operations.Containable allows you to streamline and simplify operations on
your model bindings. It works by temporarily or permanently altering the
associations of your models.
To use the new behavior, you can add it to the
$actsAs property of your model:
class Post extends AppModel {
public $actsAs = array('Containable');
}
You can also attach the behavior on the fly:
$this->Post->Behaviors->load('Containable');
You can also invoke
Containable’s magic from inside the find() call:
$this->Post->find('all', array('contain' => false));
$this->Post->find('all', array('contain' => 'Comment.author'));
You can also filter the
associated Comment data by specifying a condition:
$this->Post->find('all', array('contain' => 'Comment.author = "Daniel"'));
f you want to filter the
posts by the comments, so that posts without a comment by Daniel won’t be
returned, the easiest way is to find all the comments by Daniel and contain the
Posts.:
$this->Comment->find('all', array(
'conditions' => 'Comment.author = "Daniel"',
'contain' => 'Post'
));
Additional filtering can
be performed by supplying the standard find options:
$this->Post->find('all', array('contain' => array(
'Comment' => array(
'conditions' => array('Comment.author =' => "Daniel"),
'order' => 'Comment.created DESC'
)
)));
$this->User->find('all', array(
'contain' => array(
'Profile',
'Account' => array(
'AccountSummary'
),
'Post' => array(
'PostAttachment' => array(
'fields' => array('id', 'name'),
'PostAttachmentHistory' => array(
'HistoryNotes' => array(
'fields' => array('id', 'note')
)
)
),
'Tag' => array(
'conditions' => array('Tag.name LIKE' => '%happy%')
)
)
)
));
No comments:
Post a Comment