May 28, 2014

9:11 am
In this article, we are going to discuss about How to use unBindModel() in CakePHP and the purpose of using unBindModel() in CakePHP. If you have a lot of associations in your model then it take a lot of time to return result. So to remove the unnecessary model in cakephp. we can use unBindModel(). By using unBindModel we can easily load a page more faster.

Sometimes you don't want all that information to be returned in a query which are associated in a model. The answer to this is to use the unBinModel() function. Using this you can unbind certain parts of your associations. The easiest way to demonstrate this is with an example.

If you have a user model with the following associations:

var $hasMany = array(
'Worksheet' => array('className' => 'Worksheet'
),
'Login' => array('className' => 'Login'
),
'Expense' => array('className' => 'Expense'
)
);
var $belongsTo = array(
'Level' =>
array('className' => 'Level'),
'County' =>
array('className' => 'County'),
'Province' =>
array('className' => 'Province')
);

That is potentially a lot of information with array of [Worksheet,Login,Expense,Level,Country,Province] each time you query the User.

So if we want to unbind Model (Login and Expense) we will write

$this->User->unBindModel(array(hasMany => array('Login', 'Expense')));

and we now call the findAll() function.

$this->User->findAll();

Now the result array will contain the of [User,Worksheet,Level,Country,Province] data.

we no longer return information from the Login and Expenses tables. This can be used for any of the associations and can cut down query times dramatically.

0 comments:

Post a Comment