Yii csort

Yii sort : sort multi table field with csort
csort cheat sheet

Example here are 3 tables joins to 4th table: [result]

Model :
     $sort = new CSort();
  $criteria->with = array('module','action','testcase');
               //1.  with joins the table with table respective alias name
               so we may have to use  t.-------- field for current table

$sort->attributes = array(
'module_id'=>array(
                             //2. module id is also used in view as name but module_name is displayed field
      'asc'=>'module.module_name',
       'desc'=>'module.module_name desc',
),
'action_id'=>array(
     'asc'=>'action.action_title',
     'desc'=>'action.action_title desc',
),
'testcase_id'=>array(
     'asc'=>'testcase.testcase_title',
     'desc'=>'testcase.testcase_title desc',
),
'*',
);

$sort->defaultOrder=array('module_id' => CSort::SORT_ASC,'action_id' => CSort::SORT_DESC,'testcase_id' => CSort::SORT_DESC);
//3. set default  sort order, it has  array of multiple field  that is sort by first iten then sort by next.

return new CActiveDataProvider($this, array(
'pagination'=>array(
                'pageSize'=>BE_ROW_PER_PAGE_10,
             ),
'sort'=>$sort, //4. pass the $sort value 
'criteria'=>$criteria
));

-------------------------------------------------------------------------------------

View File

  ..............
'columns'=>array(
  ........
// module_id is field name in table join to module table and get module_name 
            array(
                'name'=>'module_id',
                'value'=>'($data->module)?$data->module->module_name:""',
                'headerHtmlOptions'=>array('style'=>'width:200px;text-align:center;'),
                'htmlOptions'=>array(),
                ),
// or we can use virtual name that is not actual field in table ,create name in model like action_name here public $action_name;
//action_name is used in sort attribute like action_id used here
            array(
                'name'=>'action_name',
                'value'=>'($data->action)?$data->action_title:""',
                'headerHtmlOptions'=>array('style'=>'width:200px;text-align:center;'),
                'htmlOptions'=>array(),
                ),
    ...........


Share this

Related Posts

Previous
Next Post »