CakePHP 2.1 saving HABTM fields

和自甴很熟 提交于 2019-11-28 11:40:24
Borislav Sabev

Firstly your data should look like this (provided you want to save it through the user Model):

$this->request->data = array(
    'User' => array(
        'id' => '2',
    ),
    'Movie' => array(
        'Movie' => array(
            (int) 0 => '3',
        ),
    ),
);

The main mistake I see is that you're trying to save through the Join Model, where you should be saving through the User model. So in the controller use:

$this->User->saveAll($this->request->data)

If the data is as described above it should go fine. I thought I've answered your question here.

Hope this helps. Cheers!

Your associations seem interesting. Is there a reason you've associated the HABTM join model with the two models (user, movie)? You can achieve the same effect by putting the first $HABTM in the Movie model, and the second $HABTM in the User model. You don't need to create the intermediate join model, cake will do that for you, you only need the table.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!