Question

I've to retrieve a model with his related. For example, i've a table game and an other one bet. bet has a column with a game_id

For now, i retrieve only the game model, not the related bet. i try too a left join without success Here is what i did

$criteria=new CDbCriteria;
            $criteria->select = '*';
            $criteria->alias = 'Game';
            $criteria->join='INNER JOIN bet ON Game.id=bet.game_id';
            //$criteria->condition='bet.user_id='.$_userId.' or user_id is null';
            $criteria->order = 'date_game ASC';
            return $this->model()->findAll($criteria);

Thanks for your help

Was it helpful?

Solution 2

The model return only attributes have on model .If you want extend the related table you need to use with

in your model

  public function relations()
  {
    return array(
             'benmodel'=>array(self::HAS_MANY, 'Ben', 'game_id'=>'id');

            );
   }

in your controller

        return $this->model()->with('benmodel')->findAll();  //you get both table columns

OTHER TIPS

i modify relation in game controller

'bet' => array(self::HAS_MANY, 'Bet', 'game_id'),

Now i can have the game with related bet by user.

return $this->model()->with(array('bet'=>array('condition'=>'user_id='.$_userId.' or user_id is null')))->findAll($criteria);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top