bindModel and unbindModel:
bindModel() function is used to create association between models when needed and unbindModel() function is used to destroy the created association when it is no longer required. Association are used when we need data from more than one model then association comes handy to fetch data from multiple models
Code to create association using bindModel() function:
$this->Country->bindModel(
array(
'hasMany'=>array(
'State' =>array(
'className' => 'State',
'foreignKey'=> 'country_id',
)
)
)
);
$this->Country->State->bindModel(
array(
'belongsTo'=>array(
'Country' =>array(
'className' => 'Country',
'foreignKey'=> 'country_id',
)
)
)
);
$this->Country->State->bindModel(
array(
'hasMany'=>array(
'City' =>array(
'className' => 'City',
'foreignKey'=> 'state_id',
)
)
)
);
In the above code we are creating association between Country Model, State Model and City Model.
Code to destroy association using unbindModel() function:
$this->Country->unBindModel(array('hasMany'=>array('State')));
The above code will destroy association between State Model and Country Model.
0 Comment(s)