本文介绍了CakePHP:绑定模型不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有User HABTM专业。
在用户编辑中,有一个职业复选框的列表。
当我在用户模型中定义HABTM关系时工作。
但是因为这种关系正在中断其他函数,所以我将其删除并将其放在用户控制器中。

  $ this-> User-> bindModel(
array(
'hasAndBelongsToMany'=>
array(
'Profession'=>
array(
'className '=>'职业',
'joinTable'=>'professions_users',
'foreignKey'=>'user_id',
'associationForeignKey'=>'profession_id'
'unique'=> true,
'conditions'=>'',
'fields'=>'',
'order'=> ,
'limit'=>'',
'offset'=> '',
'finderQuery'=> '',
'deleteQuery'=> '',
'insertQuery'=> ''



);

绑定函数的返回值也为true。



现在当我调用$ this-> User-> saveAll($ this-> data)时,不再在professions_users表中创建行。



$ <$> find 操作,然后恢复为默认关联。您可能认为 save 操作不会触发此操作,但如果您使用或具有 afterSave的行为



尝试通过 <$ c> false 作为 Model :: bindModel 调用的第二个参数。这将使您的即时绑定持续请求的持续时间。您可以通过调用 Model :: resetAssociations ,在 saveAll 完成后显式重置关联。


I have User HABTM Professions.In user edit, there is a list of checkboxes of professions.It was working when I defined the HABTM relationship in user model.But as that relationship was interrupting other functions I removed it and put this in user 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'            => ''
                    )
                )
            )
        );

The return value of that binding function is also true.

Now when I call $this->User->saveAll($this->data), rows are not created in professions_users table anymore.

Any idea?

解决方案

The default behaviour of bindModel is to exist for one find operation, then revert to the default associations. You might think a save operation wouldn't trigger this, but if you use Cake's count-caching feature, or a Behaviour with an afterSave callback that performs a find, you might be wrong.

Try passing false as a second parameter of your Model::bindModel call. This will make your on-the-fly binding last the duration of the request. You can always explicitly reset the associations after your saveAll has completed by invoking Model::resetAssociations.

这篇关于CakePHP:绑定模型不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:28