Cgridview Search Relation Throught Relation

hello,

i have the following situation:

table xyz i has a relation:




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



the user relation above has a relation:




'team' => array(self::BELONGS_TO, 'TeamType', 'teamId'),



in gridview for xyz i’m able to show the attributes of the ‘team’ table by writing: ‘user.team.name’.

but, when i want to search by the field, i get an error ‘column not found’.

I’ve added the following:

  1. attribute ‘team_name’ to the xyz model.

  2. in the search function of xyz model:

    a. $criteria->with = array(‘user’,‘user.team’);

    b. $criteria->together = true;

    c. $criteria->compare(‘user.team_name’, $this->team_name, true);

    d. in the sort object of the dataprovider:


      'user_team'=>array(

                        'asc'=>'user.team.name',

                        'desc'=>'user.team.name DESC',

                    ),

what am i doing wrong?

thanks.

Hi Sharon,

The table alias for the relation "user.team" is "team", not "user.team".

Try this:




1. attribute 'team_name' to the xyz model.

2. in the search function of xyz model: 

   a. $criteria->with = array('user','user.team');

   b. $criteria->together = true;

   // c. $criteria->compare('user.team_name', $this->team_name, true);

   c. $criteria->compare('team.name', $this->team_name, true);

   d. in the sort object of the dataprovider:

      'user_team'=>array(

             // 'asc'=>'user.team.name',

             // 'desc'=>'user.team.name DESC',

             'asc'=>'team.name',

             'desc'=>'team.name DESC',

       ),



thanks!!! :D