繁体   English   中英

在yii 1.1.x中设置关系,如果表关联两次

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

我的表Tour有2个与用户表相关的字段:user_id(Tour的所有者)和Modifyed_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'

如果我的别名格式正确吗?

尝试使用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'),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM