Model Search With Relation

I have a model in which I have the following relation defined:




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(

        	...

			'milestone_category' => array(self::BELONGS_TO, 'MilestoneCategory', 'MilestoneCategoryID'),

		);

	}



and the search function of the same model is as follows:




	public function search($id=NULL)

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$conditions=array();

		$params1=array();

		$params2=array();

		


		if (isset($id)) {

			$conditions[]='ApplicantID=:ApplicantID';

			$params1=array('ApplicantID'=>$id);

		}

		

		if (isset($_GET['dropDownStatus']))  {

        	$criteria->with = 'milestone_category';


        	$conditions[]='milestone_category.StatusID=:StatusID';

        	$params2=array('StatusID'=>$_GET['dropDownStatus']);

    	}


    	$criteria->condition=implode(' AND ', $conditions);

		$criteria->params=array_merge($params1, $params2);

    	...




When viewing the result of this search for the first time, at which point the $_GET variable has not been set, then all rows are returned, ie, the second condition where I filter with the relation ‘milestone_category’ has not been applied.

Then, when I call this function with the $_GET variable defined, I get the expected data set.

The problem is that, once I have called this function with the $_GET variable set, then if I call this function again with the $_GET variable not set, it is not returning the full data set anymore, as if the relation ‘milestone_category’ is still active…

Is there a way of ‘unsetting’ this relation, or condition ? To be honest, I am not sure what is the problem, and where to start looking…

I am not sure if I can given you the exact answer based on what you have shown here, but I have some suggestions for a solution:

  • My first guess is that in your first view of the page it does not use the search function for getting the data. That could cause the tree different situations you mention.
  • Something else to note, the variable $id might never be NULL, maybe you should check it for empty() instead?

Like this: if (!empty($id)) { …

Hi Paul, thanks for responding.

The answer to your first suggestion is that yes, it is using the search function, as the result set that I am referring to is from the same gridview:




$this->widget('yiiwheels.widgets.grid.WhGridView',array(

	'id'=>'applicant-milestones-grid',

	'fixedHeader' => false,

	'type' => TbHtml::GRID_TYPE_CONDENSED, //'striped',

	'dataProvider' => $model->search($applicant_id),

	'responsiveTable' => true,

	'template' => "{items}",

	'columns'=>array(

    	...



I was not 100% clear on which GET variable I was referring to - sorry. It is the second variable that I was talking about:




	$_GET['dropDownStatus']



and right now I am testing it this way, but I still have the same problem :




	if ((isset($_GET['dropDownStatus'])) && ($_GET['dropDownStatus']!==NULL)) { ...