【发布时间】:2011-04-08 22:01:06
【问题描述】:
我想动态创建从一个模型到我的用户模型的绑定,以便每次在该模型上执行查找时都不会调用 JOIN。我正在使用绑定来执行 HABTM 保存。但是,当我使用 bindModel 函数时,HABTM 数据并没有保存在数据库中。
奇怪的是,如果我将绑定移动到 User 模型,那么保存就完美了。我在文档中没有看到任何迹象表明,当在模型中与 bindModel 函数建立关联时,保存行为会有所不同(不过,如果有的话,我可能会错过它)。
这是我的控制器中的 bindModel 代码:
$this->User->bindModel(
array(
'hasAndBelongsToMany' => array(
'Othermodel' => array(
'className' => 'Othermodel',
'joinTable' => 'othermodels_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'othermodel_id',
'unique' => true,
)
)
)
);
if($res = $this->User->save($data)){
return true;
}
这是我的用户模型。
class User extends AppModel {
public $name = 'User';
public $belongsTo = array();
public $hasOne = array();
public $hasMany = array();
public $hasAndBelongsToMany = array(
'Othermodel' => array(
'className' => 'Othermodel',
'joinTable' => 'othermodels_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'othermodel_id',
'unique' => true
)
);
再次,我一次只在一个地方激活关系,所以我知道问题不在于绑定本身。看来问题仅与我尝试使用 bindModel 的事实有关。这是预期的行为吗?
【问题讨论】:
标签: cakephp cakephp-1.3