CakePHP 2.1节省HABTM领域 [英] CakePHP 2.1 saving HABTM fields
问题描述
我有两个模型用户和电影..这些与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'=>''
))
我在UsersController中创建了'watchlist'操作。代码如下
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电影已添加到您的观看列表','admin / flash_success');
$ this-> redirect($ this-> referer(array('action'=>'listing')));
} else {
$ this-> Session-> setFlash('无法将电影添加到您的观看列表中,请重试。','admin / flash_error');
$ this-> redirect($ this-> referer(array('action'=>'listing')));
}
}
所以我得到一个错误,告诉我解决方案
首先你的数据应该看起来像这样(假如你想通过用户模型保存):
$ this-> request-> data = array(
'User'=& $ b'id'=>'2',
),
'Movie'=> array(
'Movie'=> array(
=>'3',
),
),
);
我看到的主要错误是你试图通过Join模型保存,通过用户模型进行保存。因此在控制器中使用:
$ this-> User-> saveAll($ this-> request->数据)
如果数据如上所述,应该就好了。我想我已经回答了您的问题这里。 p>
希望这有帮助。
干杯!
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屋!