tags:

views:

342

answers:

1

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?

+2  A: 

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.

deceze
in this case is it good practice to unbind like this?? `unset($this->User->hasAndBelongsToMany['Friend])`
RSK
@RSK That would either do nothing, or break things horribly. I wouldn't want to try either way.
deceze
@deceze : http://stackoverflow.com/questions/3707859/cakephp-validating-a-login-form-using-validate-array can u plz answer for this
RSK
@RSK No, because I don't see any need to validate login information. The login is either correct or it isn't, giving any more details only helps potential attackers. I have written about this here before somewhere, but can't find it right now.
deceze
@deceze : thankz alot for u'r time :-)
RSK