Search With Filter On Count

Hi and Happy 2014 to everyone!

I have a little question:

This is part of my model Peoples.

Here we have relations of Peoples with Events model.




	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(

		    'events'=>array(self::HAS_MANY, 'Events', 'people_id'),

		);

	}



And search method




	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria = new CDbCriteria();

		$criteria->alias = 'p';

		$criteria->select = "p.*, c.name AS city_name";

		$criteria->join = "JOIN tbl_cities AS c ON p.city_id = c.id";

		$criteria->addSearchCondition('p.locality', (isset($_GET['search']) ? $_GET['search'] : null), true, 'OR');

		$criteria->addSearchCondition('c.name', (isset($_GET['search']) ? $_GET['search'] : null), true, 'OR');

		$criteria->order = 'p.id DESC';

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'pagination' => array(

				'pageSize' => 20,

				'pageVar'=>'page'

			)

		));

	}



Now part of _search view




	<div class="form-group">

		<div class="col-lg-12">

			<input size="24" maxlength="255" class="form-control inline-control" placeholder="People, City, Locality" name="search" id="Peoples_search" type="text" value="<?php echo isset($_GET['search']) ? $_GET['search'] : ''; ?>" />

			<?php echo CHtml::submitButton('Search', array('class'=>'btn btn-primary')); ?>

		</div>

	</div>	

	<div class="form-group">

		<div class="col-lg-12">

			<div class="checkbox">

				<?php echo CHtml::checkbox('Peoples_not_empty', (isset($_GET['Peoples_not_empty']) ? $_GET['Peoples_not_empty'] : '')); ?>

				<?php echo CHtml::label('only Peoples with events', 'Peoples_not_empty'); ?>

			</div>

		</div>

	</div>	




My search method works. Now I would add another condition: if checkbox is checked I have to add AND condition with peoples having atleast one event. I’ve not found anything similar on forum.

Anyone can help me?

Thanks!

Is this question a little complex? :-[

Hi pippo30,

I hope the following wiki article will be a help (or at least a hint) to you.

http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview

Very thanks! I’ve found the right way starting from your link

Well, i’m here again, beacause of a small thing to change.

I’ve added a province select input, where I get an Id (province_id)




                $criteria = new CDbCriteria();

                $criteria->alias = 'p';

                $criteria->select = "p.*, c.name AS city_name";

                $criteria->join = "JOIN tbl_cities AS c ON p.city_id = c.id";

                $criteria->addSearchCondition('p.locality', (isset($_GET['search']) ? $_GET['search'] : null), true, 'OR');

                $criteria->addSearchCondition('c.name', (isset($_GET['search']) ? $_GET['search'] : null), true, 'OR');

                $criteria->addSearchCondition('pr.id', (isset($_GET['province']) ? $_GET['province'] : null), true, 'AND');

                $criteria->order = 'p.id DESC';




The problem here is that the addSearchCondition method convert condition into LIKE "%string%" and if I have province_id for example 2, the search function will return every records have 2, records have 12, records have 32, etc…

How can I change this LIKE into =?

Very thanks!

I’ve found solution, using addCondition

bye, thanks