CakePHP 在 HABTM 字段中保存数据 [英] CakePHP saving data in HABTM fields

查看:31
本文介绍了CakePHP 在 HABTM 字段中保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这三个关系 User、Movie 和 UsersWatchlist.. 其中 users_watchlists 包含 movie_id 和 user_id 作为属性.. 我有这个结构

I am using these three relations User, Movie and UsersWatchlist.. Where users_watchlists contains movie_id and user_id as attributes.. I have this structure

array(
'User' => array(
    'id' => '3'
),
'UsersWatchlist' => array(
    'UsersWatchlist' => array(
        (int) 0 => '3'
    )
)

)

public function watchlist($id = null) {
    $userid = '3';
    if (!$id && $userid != 3) {
        $this->Session->setFlash('Invalid Movie');
        $this->redirect($this->referer(array('action' => 'listing')));
    }
    $this->request->data['User']['User'][] = $userid;
    $this->request->data['Movie']['Movie'][] = $id;
    debug($this->request->data);
    if ($this->User->saveAll($this->request->data)) {
        debug("saved");
        $this->Session->setFlash('The movie has been added to your watchlist', 'admin/flash_success');
        //$this->redirect($this->referer(array('action' => 'listing')));
    } else {
        debug("saved bo");
        $this->Session->setFlash('The movie could not be added to your watchlist. Please, try again.', 'admin/flash_error');
        //$this->redirect($this->referer(array('action' => 'listing')));
    }  
}

我在 User 上使用了 saveAll 方法,但它没有保存.. 请提供我保存数据的解决方案..

I used saveAll method on User but its not saving.. Please provide me solution to save the data..

在电影模型中,

public $hasAndBelongsToMany = array('User' => array(
        'className' => 'User',
        'joinTable' => 'users_watchlists',
        'foreignKey' => 'movie_id',
        'associationForeignKey' => 'user_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

在用户模型中,

public $hasAndBelongsToMany = array(
   'Movie' => array(
        'className' => 'Movie',
        'joinTable' => 'users_watchlists',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'movie_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )

);

在用户观察列表模型中,

In UsersWatchlist model,

public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Movie' => array(
        'className' => 'Movie',
        'foreignKey' => 'movie_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

推荐答案

您尝试保存的数据数组错误.

The data array you're trying to save is wrong.

如果您通过 User 模型保存,您的结构应该是:

If you're saving throung the User model your structure should be:

array(
'User' => array(
    'id' => '3'
),
'Movie' => array(
    'Movie' => array(
        (int) 0 => '3'
    )
)

这篇关于CakePHP 在 HABTM 字段中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆