Search code examples
cakephpcakephp-2.0has-and-belongs-to-many

CakePHP 2.1 saving HABTM fields


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


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!