CakePHP saving data in HABTM fields

北慕城南 提交于 2019-11-28 12:50:41

问题


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

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

In Movie model,

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

In User model,

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

回答1:


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


来源:https://stackoverflow.com/questions/10429812/cakephp-saving-data-in-habtm-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!