CakePHP 2.1 保存 HABTM 字段 [英] CakePHP 2.1 saving HABTM fields

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

问题描述

我有两个模型 User 和 Movie.. 其中那些与 UsersWatchlist 相关联..

public $hasAndBelongsToMany = array('User' => array('类名' =>'用户','joinTable' =>'users_watchlists','外键' =>'电影_id','associationForeignKey' =>'用户身份','独特' =>'保持现有','条件' =>'','字段' =>'','订单' =>'','限制' =>'','偏移' =>'','finderQuery' =>'','deleteQuery' =>'','插入查询' =>''))公共 $hasAndBelongsToMany = 数组('电影' =>大批('类名' =>'电影','joinTable' =>'users_watchlists','外键' =>'用户身份','associationForeignKey' =>'电影_id','独特' =>'保持现有','条件' =>'','字段' =>'','订单' =>'','限制' =>'','偏移' =>'','finderQuery' =>'','deleteQuery' =>'','插入查询' =>''))公共 $belongsTo = 数组('用户' =>大批('类名' =>'用户','外键' =>'用户身份','条件' =>'','字段' =>'','订单' =>''),'电影' =>大批('类名' =>'电影','外键' =>'电影_id','条件' =>'','字段' =>'','订单' =>''))

我在 UsersController 中创建了监视列表"操作.代码如下

public function watchlist($id = null) {$userid = 3;如果 (!$id && $userid != 3) {$this->Session->setFlash('无效电影');$this->redirect($this->referer(array('action' =>'listing')));}$this->request->data['User']['id'] = $userid;$this->User->UsersWatchlist->create();调试($this->User->UsersWatchlist->saveAll(array('user_id' =>'2', 'movie_id' => 3)));if ($this->User->UsersWatchlist->saveAll($this->request->data)) {$this->Session->setFlash('电影已添加到您的关注列表', 'admin/flash_success');$this->redirect($this->referer(array('action' =>'listing')));} 别的 {$this->Session->setFlash('该电影无法添加到您的观看列表中.请重试.', 'admin/flash_error');$this->redirect($this->referer(array('action' =>'listing')));}}

所以我在保存时遇到错误..请告诉我解决方案

解决方案

首先你的数据应该是这样的(前提是你想通过用户模型保存它):

$this->request->data = array('用户' =>大批('id' =>'2',),'电影' =>大批('电影' =>大批((int) 0 =>'3',),),);

我看到的主要错误是您试图通过加入模型进行保存,而您应该通过用户模型进行保存.所以在控制器中使用:

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

如果数据如上所述,它应该没问题.我想我已经在此处回答了您的问题.>

希望这会有所帮助.干杯!

I have two models User and Movie.. Where those are associated with UsersWatchlist..

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' => ''
))
public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Movie' => array(
        'className' => 'Movie',
        'foreignKey' => 'movie_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ))

I created 'watchlist' action in UsersController.. The code is below

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']['id'] = $userid;
    $this->User->UsersWatchlist->create();
    debug($this->User->UsersWatchlist->saveAll(array('user_id' => '2', 'movie_id' => 3)));
    if ($this->User->UsersWatchlist->saveAll($this->request->data)) {
        $this->Session->setFlash('The movie has been added to your watchlist', 'admin/flash_success');
        $this->redirect($this->referer(array('action' => 'listing')));
    } else {
        $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')));
    }  
}

So i am getting an error while saving.. Please tell me the solution

解决方案

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!

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

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