我的表格游览有两个相关用户表的字段:user_id(游览所有者)和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'),
)
}
我收到错误:
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'),