The paginate in CakePHP's controller is a function that gets paged data. With the helper's paginator, it's easy to make a page list, sort the list page.
But when I started learning to use cakephp, I had a problem that always bothered me.
How does model disassociate (unbind)?
Under normal circumstances, just release (unbind) the model I don't need before find. You can not search for these modeld associated data tables. And will automatically return after the find is finished the model that I released once again associated. The following are common use methods
User model
Class User extends Appmodel {
var $name = ' User ';
var $belongsTo = Array (
' Profile ' = Array (' className ' + ' profile ', ' foreignkey ' = ' user_id ')
)
}
Run the following code
$this->user->unbind (Array (' Belongsto ' =>array (' profile '));
$rs = $this->user->find ();
$rs would be
Array
' User ' =>array (),
)
If Unbind is not running before find, $rs will be
Array
' User ' =>array (),
' Profile ' =>array ()
)
But if you run paginate, you won't get the same result.
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 disassociate (unbind) in paginate?
The reason is that when you get the data in Find, find uses model->resetassociations (), and all the Associations (association) are restored. And paginate used two times to find. One time is to get total, Another time to get the paginated data. So the returned result is still the content of the profile.
Workaround: Assign a non-ture value to the second parameter of the Unbind. If the second parameter of unbind is true,cakephp will save the database that needs to disassociate to model->__backassociation, when run model- >resetassociations (); The associated data is restored from the model->__backassociation. So the following code can be resolved
$this->user->unbind (Array (' Belongsto ' =>array (' profile '), false);
$rs = $this->paginate (' User ');
In addition, if you run Paginate (), you also need to use the associated data in model to find data. You can add the following code to the app_model.php file
/**
* Function Description:turn off the Association,and return the The association.
* The function working for Controller->paginate () and Model->bind ().
* The function would 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
* <code>
* $this->models->unbind ($params, false);
* </code>
* @Date: 2008-10-10
* <code>
* $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.
* </code
* @param (type) parameter name: Description
**/
Function Unbindandpushmodels ($params)
{
$backAssociation =array ();
foreach ($params as $assoc = $models)
{
foreach ($models as $model)
{
If (Isset ($this->{$assoc}[$model]))
{
$backAssociation [$ASSOC] [$model] =;
unset ($this->{$assoc}[$model]);
}
}
}
Return $backAssociation;
The above is cakephp how to use unbind content in Paginate, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!