2014-07-12 71 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'),