我在UserBan模型定義了以下關係:Yii的倍數關係到同桌
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'User', 'userId'),
'target' => array(self::BELONGS_TO, 'User', 'targetId'),
'author' => array(self::BELONGS_TO, 'User', 'authorId'),
);
}
現在,當我嘗試這樣做:
$criteria->with = array('user', 'target');
它尖叫以下,因爲用戶mdel與暱稱具有默認範圍關係:
Not unique table/alias: 'nicknames'
SQL:
SELECT COUNT(DISTINCT `t`.`id`) FROM `userban` `t`
LEFT OUTER JOIN `user` `user` ON (`t`.`userId`=`user`.`id`)
LEFT OUTER JOIN `nickname` `nicknames` ON (`nicknames`.`userId`=`user`.`id`)
LEFT OUTER JOIN `user` `target` ON (`t`.`targetId`=`target`.`id`)
LEFT OUTER JOIN `nickname` `nicknames` ON (`nicknames`.`userId`=`target`.`id`)
WHERE ((user.name LIKE :username) AND (:dateStart<=t.createdAt AND :dateEnd>=t.createdAt))
我該如何克服?我在哪裏「別名」我的連接表?
編輯 下面是用戶模型的默認範圍:
public function defaultScope()
{
return array(
'with' => 'nicknames',
'together' => true
);
}
作爲一個提示,也爲需要 –