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

查看:122
本文介绍了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')));
    }  
}


$ b $ p我对用户使用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' => ''
    )

);

UsersWatchlist模型,

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.

如果你保存的用户模型你的结构应该是:

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天全站免登陆