CakePHP Syntax error or access violation

I have the following ratios Users (id, name, email address, ...) movies (id, name, ...) users_watchlists (id, user_id, movie_id)

His attitude is HABTM.

Error

Error: SQLSTATE [42000]: syntax error or access violation: 1066 Not unique table / alias: 'UsersWatchlist'

SQL Query: SELECT UsersWatchlist. id, UsersWatchlist. first_name, UsersWatchlist. last_name, UsersWatchlist. display_image, UsersWatchlist. email, UsersWatchlist. password, UsersWatchlist. birthday, UsersWatchlist. gender, UsersWatchlist. group_id, UsersWatchlist. banned, UsersWatchlist. created, UsersWatchlist. modified, UsersWatchlist. id, UsersWatchlist. first_name, UsersWatchlist. last_name, UsersWatchlist. display_image, UsersWatchlist. email, UsersWatchlist. password, UsersWatchlist. birthday, UsersWatchlist. gender, UsersWatchlist. group_id, UsersWatchlist. banned, UsersWatchlist. created, UsersWatchlist. modifiedFROM reelstubs. usersAS UsersWatchlistJOIN reelstubs. usersAS UsersWatchlistON (UsersWatchlist. movie_id= 4 And UsersWatchlist. user_id= UsersWatchlist. id)

Note. If you want to customize this error message, create an Application \ View \ Errors \ pdo_error.ctp

User model

    public $hasAndBelongsToMany = array(
       'UsersWatchlist' => array(
            'className' => 'Movie',
            'joinTable' => 'users_watchlists',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'movie_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
  );

Movie model

public $hasAndBelongsToMany = array(
'UsersWatchlist' => array(
        'className' => 'User',
        'joinTable' => 'users_watchlists',
        'foreignKey' => 'movie_id',
        'associationForeignKey' => 'user_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

UsersWatchlist

public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Movie' => array(
        'className' => 'Movie',
        'foreignKey' => 'movie_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

Not sure why he is trying to get a name, etc. from UsersWatchlist

+3
source share
1 answer

Not sure why he is trying to get a name, etc. from UsersWatchlist

Since you have a model for the datatable relationship, called UsersWatchlist, and you named the relationship between User and Movie with the same name.

Try updating these names:

User model

public $hasAndBelongsToMany = array(
   'Movie' => array(
   ...

Movie model

public $hasAndBelongsToMany = array(
   'User' => array(
   ...

These keys are used as aliases in the SQL query, which leads to failure in this case.

, users_watchlists datatable (, ), UsersWatchlist .

+2

All Articles