model - Yii multiple relations to same table -
i have following relations defined in userban model:
public function relations() { // note: may need adjust relation name , related // class name 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'), ); } now when try do:
$criteria->with = array('user', 'target'); it screams following because user mdel has default scoped relation nicknames:
not unique table/alias: 'nicknames' sql:
select count(distinct `t`.`id`) `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`) ((user.name :username) , (:datestart<=t.createdat , :dateend>=t.createdat)) how on this? "alias" joined tables ?
edit here default scope user model:
public function defaultscope() { return array( 'with' => 'nicknames', 'together' => true ); }
when define multiple relations same table it's idea specify unique aliases each one. when specifying relations:
return array( 'user' => array(self::belongs_to, 'user', 'userid', 'alias' => 'unick'), 'target' => array(self::belongs_to, 'user', 'targetid', 'alias' => 'tnick'), 'author' => array(self::belongs_to, 'user', 'authorid', 'alias' => 'anick'), ); see documentation cactiverecord::relations more information.
update: seems need use different aliases joining nicknames table in default scope. 'm not sure best way be, works , it's easy do:
public function defaultscope() { static $counter = 0; return array( 'with' => array( 'nicknames' => array('alias' => 'nick'.($counter++)) ), 'together' => true, ); }
Comments
Post a Comment