How does Cakephp use unbind in paginate?

Source: Internet
Author: User
How does Cakephp use unbind in paginate?

Paginate in the controller of cakephp is a function for obtaining paging data. with Paginator in helper, you can easily create paging lists and sorted list pages.
But when I started learning how to use cakephp, I had a problem that had always plagued me.
How do I unbind a Model )?
Under normal circumstances, as long as the model is removed (unbind) I don't need before find. you can not search for the data tables associated with these modeld. after finding is complete, the previously released model will be automatically re-associated. the following are common usage methods:


// User model
Class User extends AppModel {

Var $ name = 'user ';
Var $ belonsto = array (
'Profile '= array ('classname' => 'Profile', 'foreignkey' => 'User _ id ')
)
}

Run the following code:

$ This-> User-> unbind (array ('belonsito' => array ('Profile ')));
$ Rs = $ this-> User-> find ();

$ Rs will be

Array (
'User' => array (),
)

If unbind is not run before find, $ rs will be

Array (
'User' => array (),
'Profile '=> array ()
)

However, if paginate is run, the same results will not be obtained.
Code]
$ This-> User-> unbind (array ('belonsito' => array ('Profile ')));
$ Rs = $ this-> paginate ('user ');
[/Code]
The result of $ rs is still

Array (
'User' => array (),
'Profile '=> array ()
)

Why can't I unbind a link in paginate )?
The reason is that after the data is obtained in find, find will use model-> resetAssociations (); to restore all associations. in paginate, find is used twice. one is the total number, and the other is the data displayed by page. therefore, the returned results still contain the Profile content.
Solution:Assign a non-ture value to the second parameter of unbind. if the second parameter of unbind is true, cakephp will save the database to be unbound to model->__ backAssociation. when model-> resetAssociations () is run (); the associated data will be restored from model-> __backassociation. so the following code can solve the problem:

$ This-> User-> unbind (array ('belonstamp' => array ('Profile '), false );
$ Rs = $ this-> paginate ('user ');

In addition, if you need to use the associated data in the model to find data after running paginate (), you can add the following code in the app_model.php file:


/**
* Function description: turn off the Association, and return 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
*
   *                      $this->Models->unbind($params,false);
   *                      

* @ Date: 2008-10-10
*
* $ BackAssociation = $ this-> ModelName-> unbindAndPushModels (array ('belonsito' => array ('user ')));
* $ Result = $ this-> paginate ('modelname ');
* $ This-> ModelName-> bind ($ backAssociation); // this action is to restore the model of assocication data.
** @ 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.