Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unbindModel call in CakePhp. How does it work?

Tags:

php

cakephp

How does unbindModel happen in cake?

$this->User->unbindModel(array('hasAndBelongsToMany' => array('Friend')));

I wrote this in the beginning of a function. But still it queries the 'Friend' model. There was a call to paginate() in the middle of the function. So I thought the paginator might be generating the queries.

I did added an unbindModel call just before paginate and it now works.

$this->User->unbindModel(array('hasAndBelongsToMany' => array('Friend')));
$user = $this->paginate("User", array("User.first_name LIKE" => $user["User"]["first_name"]));

Does unbindModel unbind every query? or does it unbind during the entire function call?

like image 522
zero juan Avatar asked Oct 19 '09 06:10

zero juan


1 Answers

From the manual:

Removing or adding associations using bind- and unbindModel() only works for the next model operation unless the second parameter has been set to false. If the second parameter has been set to false, the bind remains in place for the remainder of the request.

In other words, after you paginate() or find() or do anything else with the model, the unbinding will be reversed.

like image 151
deceze Avatar answered Oct 20 '22 01:10

deceze