Sometime we need to create association between models .we need this because ,we want that all associated data came on the first level of recursion.
In cakephp creation of association is done by using cakephp bindModel() method.
In below code we are creating association between country model and state model.
public function bind(){
$this->Country->bindModel(
array(
'hasMany'=>array(
'State' =>array(
'className' => 'State',
'foreignKey' => 'country_id',
)
)
)
);
$data = $this->Country->find("all", array('limit'=>4));
}
when we debug above code byusing below method:-
debug($data);
It will gives us data of each country which is store in database with it associated state data.
0 Comment(s)