Searching And Sorting By Count Of Related Items In

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!

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. :)

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