Return to Snippet

Revision: 58418
at July 13, 2012 10:08 by drock


Initial Code
//AT THE END OF YOUR MODEL SEARCH FUNCTION

return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination' => array(
						'pageSize' =>15,
				),
				'sort'=>array(
						'defaultOrder'=> array('id'=>CSort::SORT_ASC), //Can add more here, but arrows show
				
						'attributes'=>array(
								'id',
								'followers_count'=>array(
										'desc' => '(SELECT SUM(graph_edge.id) FROM graph_edge WHERE graph_edge.end = t.id AND graph_edge.status = 1) DESC',
										'asc' => '(SELECT count(graph_edge.id) FROM graph_edge WHERE graph_edge.end = t.id AND graph_edge.status = 1) ASC', //This query should match your STAT relationship
								),
								'create_time'=>array(
										'desc' => 't.create_time DESC',
										'asc' => 't.create_time ASC',
				
								),
								'page_title',
						),
				),
		));

Initial URL

                                

Initial Description
When setting up the AJAX click to sort links for your index pages, it's nice to be able to sort on STAT and related data. 

To get related data sorting in my CListView, I ended up having to forget about using "together" and "with" in my search function, and instead let everything lazy load. The number of queries explode, which sucks.

For handling your STAT relations, you can manually write SQL that runs when the sorting links are clicked. So this is pretty much duplicate code and sloppy, but it works.

The query below recreated this relation that exists on the model:
<code>
'followersCount'=>array(self::STAT,'GraphEdge','end','condition'=>'status=1'),
</code>

The naming here doesn't matter, the relation and sortable attribute are unfortunately independent. This was written for CListView but probably works with CGridView as well.
Don't forget to add followers_count to your List View's sortable attributes array.

Initial Title
Yii: Ajax sorting on STAT relations

Initial Tags

                                

Initial Language
PHP