在yii 1.1.如果表关联两次,则X集关系


In yii 1.1.x set relations if table is related twice

我的表Tour有两个相关的字段:user_id(Tour的所有者)和modified_by(最后修改的人)问题是我不知道如何与他们建立关系。如果我写:

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'),
  )
}

I get error:

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'

我的别名格式是否正确?

try with alias name

like that

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'),