I’m trying to make CGridView to sort a particular column, but so far I’ve been unsuccessful, can anybody figure out the problem?
in model:
public function relations()
{
return array(
'stats' => array(self::HAS_ONE,'CustomerStat','customerId'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
$criteria->compare('city',$this->city,true);
$criteria->compare('type',Customer::TYPE_CUSTOMER);
$criteria->with = array('stats');
return new CActiveDataProvider('Customer', array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'id DESC',
'attributes' => array(
'name',
'city',
'stats.balance',
),
),
'pagination' => array(
'pageSize' => CustomerController::PAGE_SIZE,
),
));
}
and in the view:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$customer->search(),
'filter' => $customer,
'columns'=>array(
'name',
'city',
array(
'header' => 'Balance',
'name' => 'stats.balance',
'sortable' => true, //does not sort
),
'stats.balance:text:Balance', //does not sort
$buttons,
),
));
The columns “name” and “city” get sorted just fine, the application log shows the SQL to sort the fields just fine. But whenever I clicked the stats.balance the results get sorted based on the ‘defaultOrder’ attribute. Any ideas?
cheers
Ari