2014-07-12 50 views
0

我的表Tour有兩個相關的字段用戶表user_id(遊覽所有者)和modified_by(誰最後修改了它) 問題是我不知道如何與他們建立關係。 如果我寫:在yii 1.1.x中設置關係如果表關聯兩次

public function relations() 
{ 
    return array(
    'belongs_to_modified_by_user' => array(self::BELONGS_TO, 'User', 'modified_by'), 
    'belongs_to_user_id_user' => array(self::BELONGS_TO, 'User', 'user_id'), 
) 
} 

我得到錯誤:

Syntax error or access violation: 1066 Not unique table/alias: 'user' 

如何正確?

我特林讓它,但它失敗:

public function relations() 
{ 
    return array(
    'belongs_to_modified_by_user' => array(self::BELONGS_TO, 'User', 'modified_by', 'alias' => 'modi'), 
    'belongs_to_user_id_user' => array(self::BELONGS_TO, 'User', 'user_id', 'alias' => 'touser'), 
    'belongs_to_category' => array(self::BELONGS_TO, 'Category', 'category_id'), 
    'belongs_to_region' => array(self::BELONGS_TO, 'Region', 'region_id'), 
    'belongs_to_subregion' => array(self::BELONGS_TO, 'Subregion', 'subregion_id'), 
    'belongs_to_state' => array(self::BELONGS_TO, 'State', 'state_id'), 
); 
} 

...

In method for data retrieving: 
    $criteria = new CDbCriteria; 
    $criteria->alias = 'T'; 
    $criteria->with[] = 'belongs_to_modified_by_user'; 
    $criteria->with[] = 'belongs_to_user_id_user'; 
    $criteria->with[] = 'belongs_to_category'; 
    $criteria->with[] = 'belongs_to_region'; 
    $criteria->with[] = 'belongs_to_subregion'; 
    $criteria->with[] = 'belongs_to_state'; 

But I get error : 
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'user'. The SQL statement executed was: SELECT COUNT(DISTINCT `T`.`id`) FROM `tyb_tour` `T` LEFT JOIN tyb_category as c ON c.id = T.category_id LEFT OUTER JOIN `tbl_users` `user` ON (`T`.`modified_by`=`user`.`id`) LEFT OUTER JOIN `tbl_profiles` `profile` ON (`profile`.`user_id`=`user`.`id`) LEFT OUTER JOIN `tbl_users` `user` ON (`T`.`user_id`=`user`.`id`) LEFT OUTER JOIN `tbl_profiles` `profile` ON (`profile`.`user_id`=`user`.`id`) LEFT OUTER JOIN `tyb_category` `belongs_to_category` ON (`T`.`category_id`=`belongs_to_category`.`id`) LEFT OUTER JOIN `tyb_region` `belongs_to_region` ON (`T`.`region_id`=`belongs_to_region`.`id`) LEFT OUTER JOIN `tyb_subregion` `belongs_to_subregion` ON (`T`.`subregion_id`=`belongs_to_subregion`.`id`) LEFT OUTER JOIN `tyb_state` `belongs_to_state` ON (`T`.`state_id`=`belongs_to_state`.`id`) WHERE (T.user_id=:ycp0). Bound with :ycp0='2' 

如果我的別名格式是否正確?

+0

你的代碼是正確的,這個錯誤是不相關的這些關係可言,請嘗試跟蹤您的代碼,並確切地知道行拋出這個錯誤 –

回答

1

嘗試alias name

return array(
    'belongs_to_modified_by_user' => array(self::BELONGS_TO, 'User', 'modified_by', 'alias' => 'modi'), 
    'belongs_to_user_id_user' => array(self::BELONGS_TO, 'User', 'user_id', 'alias' => 'touser'),