Cdbcriteria Doesn't Generate Table Alias


I have a model with 2 relations to the same model/table:

Class Work extends CActiveRecord


   public function relations()


      return array(

         'user' => array(self::BELONGS_TO, 'User', 'user_id'),

         'other_user' => array(self::BELONGS_TO, 'User', 'other_user_id'),



Then I have a search method that returns data provider for that model:

  public function search()


      $criteria=new CDbCriteria;

      $criteria->with = array('user','other_user','other_user.profile'); 

      return new CActiveDataProvider($this, array(




I use that search method for grid view, but it returns an error:

as you can see, the SQL doesn’t use the table alias from the relation’s name. Why?


why do you need BELONGS…TO two times with same table.


chandran nepolean

Because the table "Work" has 2 fields that relate to table "User", they are "user_id" and "other_user_id", each of them has its own meaning.

Just got it solved. I found an alias in User model’s defaultScope which caused the problem.