Search code examples
yiicriteriadataprovider

Yii DataProvider with two tables


I have two tables

User:
id | name | gender(boolean)

Gender:
gender_id (boolean) | gender_name (text)

I want to display the text representation of gender through DataProvider

UserController:
public function actionIndex()
{           
  $crt = new CDbCriteria();
  $crt->alias = 'so';
  $crt->select = 'so.id, so.name, so.gender, fl.Gender_name';   
  $crt->join = " left join " . Gender::model()->tableName() . " as fl on fl.Gender_id = so.Gender"; 

  $dataProvider=new CActiveDataProvider('User', array('criteria' => $crt));
  $this->render('index',array(dataProvider'=>$dataProvider,));
}

As a result, I can not pass through dataProvider table gender


Solution

  • You can beter create a relation in the models. This way you dont have to use the criteria, and the value canbe accessed through $model->gender->gender_name for example