我有用户 HABTM 职业。 在用户编辑中,有一个职业复选框列表。 当我在用户模型中定义 HABTM 关系时,它起作用了。 但由于这种关系中断了其他功能,我将其删除并将其放入用户 Controller 中
$this->User->bindModel(
array(
'hasAndBelongsToMany' =>
array(
'Profession' =>
array(
'className' => 'Profession',
'joinTable' => 'professions_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'profession_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
)
)
);
该绑定(bind)函数的返回值也是 true。
现在,当我调用 $this->User->saveAll($this->data) 时,不再在 professionals_users 表中创建行。
有什么想法吗?
最佳答案
bindModel 的默认行为是针对一个 find
操作而存在,然后恢复为默认关联。您可能认为保存
操作不会触发此操作,但如果您使用Cake's count-caching feature ,或者具有执行 find
的 afterSave
回调的行为,您可能是错的。
尝试将 false
作为 Model::bindModel
调用的第二个参数传递。这将使您的动态绑定(bind)在请求期间持续有效。在 saveAll
完成后,您始终可以通过调用 Model::resetAssociations
显式重置关联。
关于CakePHP:绑定(bind)模型不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4266352/