Yii Framework Forum: Searching And Sorting By Count Of Related Items In - Yii Framework Forum

Jump to content

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

Searching And Sorting By Count Of Related Items In Default value in search field Rate Topic: -----

#1 User is offline   Reinier 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 24-April 12

Posted 19 November 2012 - 08:44 AM

Hi there,
I've followed this tutorial
And it works really nice, but not completely.
The problem is that when I load the admin file the default value of the count is set at 0, so in the filter input type is a zero. Is this something by default or did I made a mistake?

List.php
* @property integer $lijstItemsCount
* @property integer $gedeeldCount

public function rules(){
   return array(
       ...,
       array('... lijstItemsCount, gedeeldCount', 'safe', 'on'=>'search'),);
}

public function relations()
	{
		return array(
			...
			
			'lijstItemsCount' => array(self::STAT, 'LijstItem', 'lijst_id'),
			'gedeeldCount' => array(self::STAT, 'KoppelUserLijst', 'list_id'),
		);
	}

public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$lijstItemTable = LijstItem::model()->tableName();
		$koppelTable = KoppelUserLijst::model()->tableName();
		$wensen = "(select count(*) from ". $lijstItemTable ." where ". $lijstItemTable .".lijst_id = t.id)";
		$gedeeld = "(select count(*) from ". $koppelTable ." where ". $koppelTable .".list_id = t.id)";
		
		$criteria=new CDbCriteria;
		$criteria->select = array(
				'*',
				$wensen . " as lijstItemsCount",
				$gedeeld . " as gedeeldCount",
			);
		
		...

		$criteria->compare($wensen, $this->lijstItemsCount);
		$criteria->compare($gedeeld, $this->gedeeldCount);
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination' => array(
				'pageSize' => 50,
			),
		));
	}


Hope someone can help.
Thanks in advance!
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 19 November 2012 - 09:07 AM

Hi Reinier,

Try defining the attributes for them independent of the STAT relations.
public $lijstItemsCount;
public $gedeeldCount;
public function relations()
	{
		return array(
			...
			
			// 'lijstItemsCount' => array(self::STAT, 'LijstItem', 'lijst_id'),
			// 'gedeeldCount' => array(self::STAT, 'KoppelUserLijst', 'list_id'),
		);
	}

If you want to continue to use the STAT relations, use different names.
public $lijstItemsCountEx;
public $gedeeldCountEx;
public function relations()
	{
		return array(
			...
			
			'lijstItemsCount' => array(self::STAT, 'LijstItem', 'lijst_id'),
			'gedeeldCount' => array(self::STAT, 'KoppelUserLijst', 'list_id'),
		);
	}
...


Originally I myself thought that I could share the attribute names with the STAT relations, but it turned out to be problematic. :)
0

#3 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 19 November 2012 - 12:14 PM

Dear Reinier

To avoid the problem, we have to specifically make it null in controller.
I have a model Brand.
Brand HAS_MANY relation with Item.
Property itemCount is number of items a particular brand is having.

public function actionAdmin()
	{
		$model=new Brand('search');
		$model->unsetAttributes();  
		$model->itemCount=null; //This is the added line. the method unsetAttributes() is not going to unset virtual properties.
		if(isset($_GET['Brand']))
			$model->attributes=$_GET['Brand'];
			
		$this->render('admin',array(
			'model'=>$model,
		));
	}


If you are further interested , Kindly pay attention to the following thread.

Filtering on STAT relation CGridView
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