Yii Framework Forum: CSort and self::STAT to sort by Count - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CSort and self::STAT to sort by Count Rate Topic: -----

#1 User is offline   xyber 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 24-October 09

Posted 02 February 2010 - 06:31 PM

I have been searching the forums and just can't find a solution to this.
I am trying to sort a Gallery by the number of images in it. I have a Gallery table and Images table.

// Gallery model
public function relations()
{
  return array(
  'user' => array(self::BELONGS_TO, 'User', 'owner_id'),
  'images' => array(self::HAS_MANY, 'GallImg', 'gid'),
  'imgCount' => array(self::STAT, 'GallImg', 'gid'),
  );
}


and in the controller I do
$crit = new CDbCriteria(array("with" => "imgCount"));
$pages = new CPagination(Gallery::model()->count($crit));
$pages->route = "gall/index";
$pages->pageSize = SAGD::STANDARD_LIST_SIZE;
$pages->setCurrentPage($page-1);
$pages->applyLimit($crit);
$sort = new CSort('Gallery');
$sort->multiSort = false;
$sort->attributes = array('imgCount' => 'imgCount',);
$sort->defaultOrder = 'imgCount ASC';
$sort->applyOrder($crit);
$galls = Gallery::model()->findAll($crit);


which fails with error ...
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'imgCount' in 'order clause'

0

#2 User is offline   Tobias B. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 03-August 10

Posted 03 August 2010 - 11:18 AM

I have a simmilar problem. I want to sort a CList by statistical queries. (How) did you solve this problem?
0

#3 User is offline   Tobias B. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 03-August 10

Posted 04 August 2010 - 09:50 AM

I solved it like that:


		$sort = new CSort('Thing');


		$sort->attributes =array(
			'points' => array(
				'asc'=>'numberOfPoints ASC',
				'desc'=>'numberOfPoints DESC',
			)
		);
		
		$dataProvider = new CActiveDataProvider('Thing', array(

    		'criteria'=>array(
				'select' => 't.*, COUNT(point.id) AS numberOfPoints',
				'join' => 'LEFT JOIN points_table point ON point.thing_id = t.id',
				'condition' => 'published_at IS NOT NULL',
				'group' => 't.id',
			),
			'sort'=>$sort,
			'pagination'=>array('pageSize' => 3,)
		));


and in the view:

$this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_listEntry',
	'sortableAttributes'=>array(
		'points',
	),
));

0

#4 User is offline   Vishal Rathod 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 25-November 11
  • Location:Pune,India

Posted 23 May 2012 - 06:09 AM

View PostTobias B., on 04 August 2010 - 09:50 AM, said:

I solved it like that:


		$sort = new CSort('Thing');


		$sort->attributes =array(
			'points' => array(
				'asc'=>'numberOfPoints ASC',
				'desc'=>'numberOfPoints DESC',
			)
		);
		
		$dataProvider = new CActiveDataProvider('Thing', array(

    		'criteria'=>array(
				'select' => 't.*, COUNT(point.id) AS numberOfPoints',
				'join' => 'LEFT JOIN points_table point ON point.thing_id = t.id',
				'condition' => 'published_at IS NOT NULL',
				'group' => 't.id',
			),
			'sort'=>$sort,
			'pagination'=>array('pageSize' => 3,)
		));


and in the view:

$this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_listEntry',
	'sortableAttributes'=>array(
		'points',
	),
));



Thanks a lot. Its very helpful for me as I am implementing this first time. :)
0

#5 User is offline   dannythebestguy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 27-June 12

Posted 09 October 2013 - 01:17 AM

The Sorting works for me. But the filter does not work. Any solution please...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users