CDbCriteria With doesn't work in findAll

Player relation:


     * @return array relational rules.


    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(

            'akas' => array(self::HAS_MANY, 'PlayerAka', 'player_id'),



The code:

    public function actionPlayerAutoCompleteLookup()


       if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))


            /* q is the default GET variable name that is used by

            / the autocomplete widget to pass in user input


          $name = $_GET['q']; 

                    // this was set with the "max" attribute of the CAutoComplete widget

          $limit = min($_GET['limit'], 50); 

          $criteria = new CDbCriteria;

          $criteria->with = array('akas');

          $criteria->condition = "t.nickname LIKE :sterm OR akas.nickname LIKE :sterm";

          $criteria->params = array(":sterm"=>"%$name%");

          $criteria->limit = $limit;

          $userArray = Player::model()->findAll($criteria);

          $returnVal = '';

          foreach($userArray as $userAccount)


              $akas = '';

               foreach ( $model->akas as $a ) $akas .= $a->nickname .', ';

             if ( strlen($akas) > 0 ) {

                 $akas = substr($akas, 0, -2);

                 $akas = '('.$akas.')';


             $returnVal .= $userAccount->getAttribute('nickname').$akas.'|'



          echo $returnVal;




It does not use your relation name ‘akas’. Try using model’s table name or model alias.

I tried with the table name ‘player_aka’ but it doesn’t work

What can I do?

I’ve solved adding

$criteria->join = ‘LEFT JOIN player_aka ON’;

but it’s a dirty way to do it when I’ve already implemented a relationn…

i am not sure with it.

But have you set the id of player_aka to primary key?


It’s better to enable SQL queries logging and check what SQL is generated.

how can I enable it?

uncomment the line of main.php