Using unBindModel() in CakePHP

June 15, 2007

If you have a lot of associations in your model setups, sometimes you dont want all that information to be returned in a query. 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 to get back each time you query the User.

So if we define

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

before we call

$this->User->findAll();

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.

results matching ""

    No results matching ""