2012-04-30 3 views
3

사용자 (ID, 이름, 이메일, ...) 영화 (ID, 이름, ...) users_watchlists (ID 266 movie_id)CakePHP의 구문 내가 관계를 다음과 같은 한 오류 또는 액세스 위반

그것의 HABTM 관계.

오류

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 . modified FROM reelstubs . users AS UsersWatchlist JOIN reelstubs . users AS UsersWatchlist ON (UsersWatchlist . movie_id = 4 AND UsersWatchlist . user_id = UsersWatchlist . id)

Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp

사용자 모델

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

영화 모델

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

확실하지 이유는 당신이 UsersWatchlist라는 관계 데이터 테이블의 모델을하고있는 사용자와 영화 사이의 관계라는 이름 때문에 UsersWatchlist

+1

"이름"이 같은 두 개의 표가있는 것 같습니다. –

+0

동일한 모델 구조로 다른 질문을 많이 올렸지 만 답변에 언급 된 수정 사항이 포함되어있는 것으로 나타났습니다. 내 대답이 당신을 도왔다 고 결론 지을 수 있습니까? – nIcO

답변

2

Not sure why its trying to fetch name etc from UsersWatchlist

에서 이름 등을 가져 오기 위해 노력 같은 이름. 그 이름 업데이트

시도 :

사용자 모델

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

영화 모델을

public $hasAndBelongsToMany = array(
    'User' => array(
    ... 
이 키가 SQL 쿼리의 별칭으로 사용되는

, 그것은 실패하고 이 경우에.

그런데 users_watchlists 데이터 테이블을 사용하여 사용자와 영화 간 링크를 유지하는 경우 (예 : 관계에 대한 속성을 저장하지 않은 경우) UsersWatchlist 모델을 제거하면됩니다.

관련 문제