【问题标题】:CakePHP saving data in HABTM fieldsCakePHP 在 HABTM 字段中保存数据
【发布时间】:2012-05-03 10:47:11
【问题描述】:

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

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')));
    }  
}

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

在电影模型中,

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' => ''
    )

);

在 UsersWatchlist 模型中,

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

【问题讨论】:

  • 请发布您的模型以查看关系映射。
  • 我把上面的模型贴出来了..请看给我解决方案..

标签: cakephp has-and-belongs-to-many cakephp-2.1


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2013-09-24
    • 1970-01-01
    • 2012-08-08
    • 2010-12-18
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多