Aug
21
通用的unbindModel()
$this->Model->unbindModel(array('associationType' => array('associatedModelClassName')));
掌握了如何动态的解除绑定之后,让我们看看如何动态的绑定关联关系。
leaders_controller.php (partial)
funciton anotherAction()
{
//There is no Leader hasMany Principles in the leader.php model file, so
//a find here, only fetches Leaders.
$this->Leader->findAll();
//Let's use bindModel() to add a new association to the Principle model:
$this->Leader->bindModel(
array('hasMany' => array(
'Principle' => array(
'className' => 'Principle'
)
)
)
);
//Now that we're associated correctly, we can use a single find function
//to fetch Leaders with their associated principles:
$this->Leader->findAll();
}
bindModel()方法不单能创建一个关联,同样可以用来动态的修改一个关联。
下面是通常的用法:
Generic bindModel() example
$this->Model->bindModel(
array('associationName' => array(
'associatedModelClassName' => array(
// normal association keys go here...
)
)
)
);
注意:这些的前提是你的数据库表中的外键关联等已经正确设置。
[/quote]