Home > Article > Backend Development > Cakephp how to use unbind in paginate
paginate in cakephp's controller is a function that gets pagination data. With Paginator in the helper, you can easily create paginated lists and sorted list pages.
But when I started learning to use cakephp, I had a problem It has been bothering me.
How to unbind a model?
Under normal circumstances, as long as I unbind the models I don’t need before finding, I don’t need to search the data tables associated with these models. And after the find is completed, In the future, the model I previously released will be automatically associated again. The following are commonly used methods
//user model
class User extends AppModel {
var $name = 'User';
var $belongsTo = array(
'Profile' = array('className'=>'Profile','foreignKey'=>'user_id')
)
}
运行以下代码
$this->User-> ;unbind(array('belongsTo'=>array('Profile')));
$rs=$this->User->find();
$rs will be
array (
'user' = & gt; array (),
)
If you do not run unbind before find, $ rs will be
Array (
'user' = & gt; array (),
',
' Profile'=>array()
)
But you don't get the same result if you run paginate
code]
$this->User->unbind(array('belongsTo'=>array( 'Profile')));
$rs=$this->paginate('User');
[/code] The result of
$rs is still
array(
'User'=>array() ,
'Profile'=>array()
)
Why can't I unbind in paginate?
The reason is that after getting the data in find, find will use model->resetAssociations(); All associations are restored. Find is used twice in paginate. One time is to get the total number, and the other time is to get the data displayed in paging. So the returned result still has the content of Profile.
Solution: Give the second parameter to unbind Assign a non-true value to it. If the second parameter of unbind is true, cakephp will save the database that needs to be disassociated into model->__backAssociation. When running model->resetAssociations(); it will be retrieved from model-> ;__backAssociation restores the relevant associated data. So the following code can solve the problem
$this->User->unbind(array('belongsTo'=>array('Profile')),false);
$rs=$this->paginate('User');
In addition, if after running paginate(), you need to use the associated data in the model to find the data. You can add it in the app_model.php file The following code
/**
* function description:turn off the Association,and return the the Association,.
* the function working for Controller->paginate() and Model->bind().
* the function will help you that get data form Controller->paginate() before unbind some
* Association for and rebind the remove of Association after get data.
* if you don't neet to rebind Association,you can only use
* <br> * $this->Models->unbind($params,false);<br> *
* @Date:2008-10-10
*
* @param (类型)参数名 :描述
* $backAssociation = $this->ModelName->unbindAndPushModels(array('belongsTo'=>array('User')));
* $result=$this->paginate('ModelName');
* $this->ModelName->bind($backAssociation);//this action is to restore the model of assocication data.
*
**/
Function unbindAndPushModels($params)
{
$backAssociation=array();
foreach ($params as $assoc => $models)
{
foreach ( $models as $ model)
}[$model]);
} Return $backAssociation;