3

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

0

2 Answers 2

4

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!

1
  • This format does work. The ['Movie']['Movie'] is the numeric array of the IDs from the corresponding model. Commented Jan 7, 2016 at 16:26
2

Your associations seem interesting. Is there a reason you've associated the HABTM join model with the two models (user, movie)? You can achieve the same effect by putting the first $HABTM in the Movie model, and the second $HABTM in the User model. You don't need to create the intermediate join model, cake will do that for you, you only need the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.