Sorting Custom Colums In A Cgridview

Despite many searches, I’m desperately stuck with this point.

Here is the code :

model : AccountingEntry schema


    

    public function relations() {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'thirdParty' => array(self::BELONGS_TO, 'ThirdParty', 'third_party_id'),

            'account' => array(self::BELONGS_TO, 'Account', 'account_id'),

        );

    }



view : index with CGridView (simplified)


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider' => $dataProvider,

    'columns' => array(

        'entry_date:date:Date',

        'thirdParty.name:text:Tiers',

    ),

));



The second column is not clickable and does not accept sorting. After some post like this one I expected the sorting to work but it does not.

I tried to add a ‘with’ attribute to the CDataProvider like this :

controller : the actionIndex() method modified


        $dataProvider = new CActiveDataProvider('AccountingEntry', array(

                    'criteria' => array(

                        'condition' => 'account_id=' . $activeAccountId,

                        'with' => array('thirdParty'),

                        'order' => 'entry_date ASC, updated_at ASC',

                    ),

                    'pagination' => array(

                        'pageSize' => Yii::app()->params['accountingEntry']['pagination'],

                    ),

                ));



Unfortunately, I have two issues here

1 - I’m using a sort attribute on this CDataProvider and it causes a CDbException :

I don’t know how I could declare aliases here to avoid this problem.

2 - Anyway, even if I remove the ‘sort’ attribute, the ‘Tiers’ column remains unclickable…

Any help is welcome :)

For your first problem, you need to clarify which table the updated_at belongs to. You can do this by pre-pending t.column_name to the field. This, essentially, fully qualifies the column name.


'order' => 'entry_date ASC, t.updated_at ASC',

Second, you haven’t specified a sort property. You’ll notice how the sort property is configured on this post.




$sort = new CSort();

$sort->attributes = array(

	'thirdPartyName'=>array( // use this name in your grid

  	'asc'=>'thirdParty.name',

  	'desc'=>'thirdParty.name desc',

	)

);


$dataProvider = new CActiveDataProvider('AccountingEntry', array(

	criteria' => array(

		'condition' => 'account_id=' . $activeAccountId,

		'with' => array('thirdParty'),

		'order' => 'entry_date ASC, updated_at ASC',

	),

	'sort'=>$sort,

	'pagination' => array(

		'pageSize' => Yii::app()->params['accountingEntry']['pagination'],

	)

));



Matt

Hi Matt, and thanks for the reply !

Two more questions then :

1 -

It works ! But I don’t understand the magic behind. Is there any piece of documentation I could read to understand this ?

2 - about the sort property : it works also but there is a conflict with the ‘order’ property of the criteria. I found a workaround by removing this ‘order’ property and by configuring the ‘defaultOrder’ property of the CSort objet but then the sort on the ‘Tiers’ column has no effect.

Also, sorting is no more available on the other column(s). I guess I can have it back by configuring the CSort object for each sortable column but is there any more simple way to do it ?

About 2 - :

OK, never mind : I was messing some code elsewhere, that’s why the sorting action did not sort. <_<

And yes, after the CSort documentation, I’ll have to declare all the sortable columns in the ‘attributes’ attribute of the CSort object.

Here’s a small piece of info.