When we are going to retrieve records of (user) using User model, we can get the associated model’s record at the same time. That might not require at some point. To avoid this, CakePHP provides the bindModel/unbindModel methods. But this is not be a good practice. You can streamline your operation using the containable behavior. The performance and the speed will increased as well. It will mostly reduce the joining of tables.
Usage & Examples:
class User extends AppModel { public $actsAs = array('Containable'); }
Where “User” is the model for which you are adding the containable behavior.
You can also do the following on the fly:
$this->User->Behaviors->load(‘Containable’);
Operations:
Without the use of Containable
$this->User->find('all'); Here User model has hasMany relation with the Comment. [0] => Array ( [User] => Array ( [id] => 1 [title] => First article1 [content] => aaa1 [created] => 2008-05-17 00:00:00 ) [Comment] => Array ( [0] => Array ( [id] => 1 [User_id] => 1 [author] => Daniel1 [email] => [email protected] [website] => http://example.com1 [comment] => First comment1 [created] => 2008-05-17 00:00:00 ) [1] => Array ( [id] => 2 [User_id] => 1 [author] => Sam1 [email] => [email protected] [website] => http://example.net1 [comment] => Second comment1 [created] => 2008-05-10 00:00:00 ) ) ) [1] => Array ( [User] => Array (...
Using Containable:
Case 1: If we need only User data.
$this->User->contain(); $this->User->find('all'); OR $this->User->find('all', array('contain' => false)); Out Put: [0] => Array ( [User] => Array ( [id] => 1 [title] => First article1 [content] => aaa1 [created] => 2008-05-17 00:00:00 ) ) [1] => Array ( [User] => Array ( [id] => 2 [title] => Second article1 [content] => bbb1 [created] => 2008-05-10 00:00:00 ) ) Case 2: With complex associations $this->User->contain('Comment.author'); $this->User->find('all'); // or.. $this->User->find('all', array('contain' => 'Comment.author')); Out put: [0] => Array ( [User] => Array ( [id] => 1 [title] => First article1 [content] => aaa1 [created] => 2008-05-17 00:00:00 ) [Comment] => Array ( [0] => Array ( [author] => Daniel1 [User_id] => 1 ) [1] => Array ( [author] => Sam1 [User_id] => 1 ) ) ) $this->User->find('all', array('contain' => 'Comment.author = "Daniel1"')); Out put: [0] => Array ( [User] => Array ( [id] => 1 [title] => First article1 [content] => aaa1 [created] => 2008-05-17 00:00:00 ) [Comment] => Array ( [0] => Array ( [id] => 1 [post_id] => 1 [author] => Daniel1 [email] => [email protected] [website] => http://example.com1 [comment] => First comment1 [created] => 2008-05-11 00:00:00 ) ) ) [1] => Array ( [User] => Array ( [id] => 2 [title] => Second article2 [content] => bbb2 [created] => 2008-05-22 00:00:00 ) [Comment] => Array ( ) )
The gray area showing that the User data always returned irrespective of the “Comment”.
Pagination Using Containable:
Including the ‘contain’ parameter in the $paginate property we can achieve find(‘count’)
and find(‘all’) on the model. This is a most valuable feature of CakePHP.
$this->paginate['User'] = array( 'contain' => array('Comment', 'Tag'), 'order' => 'User.name' ); $users = $this->paginate('User');
If you are searching for PHP or CakePHP developers, then we are the ideal and cost savvy option for you.
Like this blog? I’d love to hear about your thoughts on this. Thanks for sharing your comments.