Shareek Ahamed
Shareek Ahamed

Reputation: 378

Yii search CDbCriteria "$sort->attributes" is not sorting "<db column> as attempts"

I need a way to sort a grid by clicking the column header...

public function search() {
    $criteria = new CDbCriteria;
    $criteria->select = 'cac.id, cac.client_name, cac.phone, cd.code, t.outgoing_call_date, (SELECT count(id) FROM css_ataps_calls WHERE caller_id = cac.id AND start_date = current_date) as attempts;

Sorting Section

$sort = new CSort();
$sort->defaultOrder = 'cac.id';
$sort->attributes = array('client_id', 'attempts'=>array('asc'=>'attempts', 'desc'=>'attempts DESC'));

SGridView Code

array('name'=>'attempts',
      'type'=>'raw',
      'value'=>'$data->attempts',
      'htmlOptions'=>array('style'=>'width:15%;text-align:center;'),
      'headerHtmlOptions'=>array('style'=>'width:15%;')
),

Upvotes: 1

Views: 480

Answers (1)

Andrey Shatilov
Andrey Shatilov

Reputation: 576

In model class:

public $attempts;

and

$dataProvider=new CActiveDataProvider('...', array(
    'sort'=>$sort),
));

and in view grid:

'columns'=>array(
    ...,
    array(
        'name'=>'attempts',      
        'htmlOptions'=>array('style'=>'width:15%;text-align:center;'),
        'headerHtmlOptions'=>array('style'=>'width:15%;')
    ),
 )

You may also need to update attributeLabels in your model to reflect the new custom field.

Upvotes: 1

Related Questions