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:
1 2 3 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | $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] => dan@example.com1 [website] => http: //example.com1 [comment] => First comment1 [created] => 2008-05-17 00:00:00 ) [1] => Array ( [id] => 2 [User_id] => 1 [author] => Sam1 [email] => sam@example.net1 [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.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | $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] => dan@example.com1 [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.
1 2 3 4 5 | $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.