Default Filter?

Hi all,

I have a project tracker page with targetdate and status fields.

By default I want to limit the list to targetdate newer than 1 month in the past OR STATUS<>“DONE”. So all the old done projects don’t clog up the initial list users see.

BUT when the user enters their own targetdate filter on the index page I want to not apply the above condition.

I’ve looked at scopes and a couple other things but I can’t seem to figure this out.

I’ve tried checking to see what filter conditions the user has applied and if there’s one for targetdate then I won’t $criteria->addCondition in my controller’s search method.

But I can’t seem to catch the $_REQUEST or find a way to detect that the user is filtering on targetdate, and it appears the filtering is not going through my search function at all. (print_r($criteria) doesn’t show me any user conditions, maybe hidden in ajax somewhere?)

Any quick pointers? I’m sure this is simple, but I can’t seem to find the right search keywords.

Can you post your model’s search() method?

From View:


$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'project-grid',

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

	'filter'=>$model,

	'rowCssClassExpression'=>'$data->color.$row%2',

	'afterAjaxUpdate' => 'reinstallDatePicker',

	'selectableRows'=>1,

	'selectionChanged'=>'function(id){ location.href="index.php?r=project/view&id="+$.fn.yiiGridView.getSelection(id); }',

	'columns'=>array(

		array(

					'type'=>'raw',

					'header'=>'Name',

					'name'=>'project_name',

					'value'=>'CHtml::link($data->project_name, array("project/view", "id"=>$data->project_prikey))',

					'htmlOptions'=>array('style'=>'font-weight:bold; text-decoration:none;'),

			),

		array(

					'name'=>'ONESTATUS',

					'htmlOptions'=>array('style'=>'font-weight:bold;'),

					'value'=>'$data->ONESTATUS',

					'filter'=>CHtml::listData(Note::getStatuses(), 'id', 'text'),

			),

		array(

					'name'=>'project_targetdate',

					'type'=>'raw',

					'headerHtmlOptions'=>array('style'=>'width:85px;'),

					'value'=>'Yii::app()->dateFormatter->format("EEE MMM d, y", $data->project_targetdate)."<BR> (".(((abs($v=(strtotime(date("Y-m-d",strtotime($data->project_targetdate)))-strtotime(date("Y-m-d")))/(3600*24)))>=7) ? round($v/7,1)." weeks" : $v." days").")"',

					'filter' => $this->widget('zii.widgets.jui.CJuiDatePicker', array(

						'model'=>$model,

						'attribute'=>'project_targetdate',

						'language' => 'en',

						'i18nScriptFile' => 'jquery.ui.datepicker.js',

						'htmlOptions' => array(

								'id' => 'datepicker_for_project_targetdate',

								'size' => '10',

						),

						'defaultOptions' => array(

								'showOn' => 'focus',

								'dateFormat' => 'yy-mm-dd',

								'showOtherMonths' => true,

								'selectOtherMonths' => true,

								'changeMonth' => true,

								'changeYear' => true,

								'showButtonPanel' => true,

								'constrainInput' => false,

							)

						),

					true),

				),

...

Search Function:


	public function search($merge=null)

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;

		

		$criteria->with = array('projectForeignCreator','projectUsers');

		$criteria->compare('projectForeignCreator.fullname', $this->projectcreator,true);

		$criteria->select .= ',j22.note_text as ONENOTE, j22.note_status as ONESTATUS';

		$criteria->join = 'LEFT JOIN (select j1.* FROM tracker_note AS j1 LEFT JOIN tracker_note AS j2 ON `j1`.`note_foreign_project`=`j2`.`note_foreign_project` AND `j1`.`note_prikey`<`j2`.`note_prikey` WHERE j2.note_foreign_project IS NULL) as j22 on j22.note_foreign_project=t.project_prikey';

		

		//$criteria->with = array('projectUsers');

		$criteria->together = true;

		$criteria->compare('projectUsers.id', $this->projectassigned);


		$criteria->compare('project_prikey',$this->project_prikey);

		$criteria->compare('project_name',$this->project_name,true);

		

		$criteria->compare('j22.note_prikey',$this->ONEKEY,true);

		$criteria->compare('j22.note_text',$this->ONENOTE,true);

		$criteria->compare('j22.note_status',$this->ONESTATUS,true);

		

		$criteria->compare('project_targetdate',$this->project_targetdate,true);

		$criteria->compare('project_description',$this->project_description,true);

		$criteria->compare('project_targetdate',$this->project_targetdate,true);

		$criteria->compare('project_foreign_creator',$this->project_foreign_creator);

		$criteria->compare('project_created',$this->project_created,true);

		

		$criteria->addCondition('project_deleted=0'); //only show non-deleted projects

		//$criteria->addCondition('note_deleted=0'); //only show non-deleted notes

		

		if($_GET['YAY']==1) print_r(Yii::app()->getRequest()->getParam('Project[project_name]', null));

		

		if($merge!==null)

		{

			$criteria->mergeWith($merge);

		}


		//This 'if' never finds any filter condition, I've tried print_r($_REQUEST) and it's not there.

//		if(strpos($criteria->condition,'targetdate')==-1)

//		{

			//$criteria->addCondition('(project_targetdate >= DATE_SUB(CURDATE(), INTERVAL 1 month) OR note_status<>"DONE")');

//		}

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'sort' => array(

				'attributes'=>array(

						'ONESTATUS' => array(

								'asc' => 'ONESTATUS',

								'desc' => 'ONESTATUS DESC',

						),

						'ONENOTE' => array(

								'asc' => 'ONENOTE',

								'desc' => 'ONENOTE DESC',

						),

						'*',

				),

				'defaultOrder' => array('project_targetdate' => false),

			),

			'Pagination' => array (

				'PageSize' => 100,

			),

		));

	}

I see that the ‘merge’ code isn’t being used at all, leftovers…

And yes, it’s EVIL, and everything else works.

I have a notes table with multiple notes per project. In the main list I only show the last most recent note with the project summary list.

There are plenty of places I could clean this up, but I don’t believe they would affect my problem, but there’s a first for everything.

I’m not sure why you’re trying to access the CDbCriteria in that way. Try something like:




if (!$this->project_targetdate)

{

    $criteria->addCondition(

        '(project_targetdate >= DATE_SUB(CURDATE(), INTERVAL 1 month) OR note_status<>"DONE")'

    );

}



I tried that and it filters the list properly for targetdate, but I can’t see what the user entered.

$this->project_targetdate is always null.

I changed the search function to show this:


		$criteria->compare('project_description',$this->project_description,true);

		$criteria->compare('project_targetdate',$this->project_targetdate,true);

		$criteria->compare('project_foreign_creator',$this->project_foreign_creator);

		$criteria->compare('project_created',$this->project_created,true);

		

		$criteria->addCondition('project_deleted=0'); //only show non-deleted projects

		//$criteria->addCondition('note_deleted=0'); //only show non-deleted notes

		

		if($_GET['YAY']==1) print_r($this);

		

		if(!$this->project_targetdate)

		{

			$criteria->addCondition('(project_targetdate >= DATE_SUB(CURDATE(), INTERVAL 1 month) OR note_status<>"DONE")');

		}

Here’s my $this var after a filter on targetdate is set.

$this->targetdate should be "2012-11-21"


Project Object ( [projectcreator] => [projectassigned] => [ONEKEY] => [ONENOTE] => [ONESTATUS] => [_md:CActiveRecord:private] => CActiveRecordMetaData Object ( [tableSchema] => CMysqlTableSchema Object ( [schemaName] => [name] => tracker_project [rawName] => `tracker_project` [primaryKey] => project_prikey [sequenceName] => [foreignKeys] => Array ( [project_foreign_creator] => Array ( [0] => user [1] => id ) ) [columns] => Array ( [project_prikey] => CMysqlColumnSchema Object ( [name] => project_prikey [rawName] => `project_prikey` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [autoIncrement] => 1 [_e:CComponent:private] => [_m:CComponent:private] => ) [project_name] => CMysqlColumnSchema Object ( [name] => project_name [rawName] => `project_name` [allowNull] => [dbType] => varchar(255) [type] => string [defaultValue] => [size] => 255 [precision] => 255 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_description] => CMysqlColumnSchema Object ( [name] => project_description [rawName] => `project_description` [allowNull] => 1 [dbType] => text [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_targetdate] => CMysqlColumnSchema Object ( [name] => project_targetdate [rawName] => `project_targetdate` [allowNull] => [dbType] => datetime [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_deleted] => CMysqlColumnSchema Object ( [name] => project_deleted [rawName] => `project_deleted` [allowNull] => 1 [dbType] => int(11) [type] => integer [defaultValue] => 0 [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_foreign_creator] => CMysqlColumnSchema Object ( [name] => project_foreign_creator [rawName] => `project_foreign_creator` [allowNull] => 1 [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => [isForeignKey] => 1 [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_created] => CMysqlColumnSchema Object ( [name] => project_created [rawName] => `project_created` [allowNull] => [dbType] => timestamp [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [_e:CComponent:private] => [_m:CComponent:private] => ) [columns] => Array ( [project_prikey] => CMysqlColumnSchema Object ( [name] => project_prikey [rawName] => `project_prikey` [allowNull] => [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => 1 [isForeignKey] => [autoIncrement] => 1 [_e:CComponent:private] => [_m:CComponent:private] => ) [project_name] => CMysqlColumnSchema Object ( [name] => project_name [rawName] => `project_name` [allowNull] => [dbType] => varchar(255) [type] => string [defaultValue] => [size] => 255 [precision] => 255 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_description] => CMysqlColumnSchema Object ( [name] => project_description [rawName] => `project_description` [allowNull] => 1 [dbType] => text [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_targetdate] => CMysqlColumnSchema Object ( [name] => project_targetdate [rawName] => `project_targetdate` [allowNull] => [dbType] => datetime [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_deleted] => CMysqlColumnSchema Object ( [name] => project_deleted [rawName] => `project_deleted` [allowNull] => 1 [dbType] => int(11) [type] => integer [defaultValue] => 0 [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_foreign_creator] => CMysqlColumnSchema Object ( [name] => project_foreign_creator [rawName] => `project_foreign_creator` [allowNull] => 1 [dbType] => int(11) [type] => integer [defaultValue] => [size] => 11 [precision] => 11 [scale] => [isPrimaryKey] => [isForeignKey] => 1 [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) [project_created] => CMysqlColumnSchema Object ( [name] => project_created [rawName] => `project_created` [allowNull] => [dbType] => timestamp [type] => string [defaultValue] => [size] => [precision] => [scale] => [isPrimaryKey] => [isForeignKey] => [autoIncrement] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [relations] => Array ( [notes] => CHasManyRelation Object ( [limit] => -1 [offset] => -1 [index] => [through] => [joinType] => LEFT OUTER JOIN [on] => [alias] => [with] => Array ( ) [together] => [scopes] => [name] => notes [className] => Note [foreignKey] => note_foreign_project [select] => * [condition] => [params] => Array ( ) [group] => [join] => [having] => [order] => [_e:CComponent:private] => [_m:CComponent:private] => ) [projectForeignCreator] => CBelongsToRelation Object ( [joinType] => LEFT OUTER JOIN [on] => [alias] => [with] => Array ( ) [together] => [scopes] => [name] => projectForeignCreator [className] => User [foreignKey] => project_foreign_creator [select] => * [condition] => [params] => Array ( ) [group] => [join] => [having] => [order] => [_e:CComponent:private] => [_m:CComponent:private] => ) [usersrelation] => CHasManyRelation Object ( [limit] => -1 [offset] => -1 [index] => [through] => [joinType] => LEFT OUTER JOIN [on] => [alias] => [with] => Array ( ) [together] => [scopes] => [name] => usersrelation [className] => ProjectUser [foreignKey] => project_user_foreign_project [select] => * [condition] => [params] => Array ( ) [group] => [join] => [having] => [order] => [_e:CComponent:private] => [_m:CComponent:private] => ) [projectUsers] => CHasManyRelation Object ( [limit] => -1 [offset] => -1 [index] => [through] => usersrelation [joinType] => LEFT OUTER JOIN [on] => [alias] => [with] => Array ( ) [together] => [scopes] => [name] => projectUsers [className] => User [foreignKey] => Array ( [project_user_foreign_user] => id ) [select] => * [condition] => [params] => Array ( ) [group] => [join] => [having] => [order] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [attributeDefaults] => Array ( [project_deleted] => 0 ) [_model:CActiveRecordMetaData:private] => Project Object ( [projectcreator] => [projectassigned] => [ONEKEY] => [ONENOTE] => [ONESTATUS] => [_md:CActiveRecord:private] => CActiveRecordMetaData Object *RECURSION* [_new:CActiveRecord:private] => [_attributes:CActiveRecord:private] => Array ( ) [_related:CActiveRecord:private] => Array ( ) [_c:CActiveRecord:private] => [_pk:CActiveRecord:private] => [_alias:CActiveRecord:private] => t [_errors:CModel:private] => Array ( ) [_validators:CModel:private] => [_scenario:CModel:private] => [_e:CComponent:private] => [_m:CComponent:private] => ) ) [_new:CActiveRecord:private] => 1 [_attributes:CActiveRecord:private] => Array ( [project_deleted] => [project_prikey] => [project_name] => [project_description] => [project_targetdate] => [project_foreign_creator] => [project_created] => ) [_related:CActiveRecord:private] => Array ( ) [_c:CActiveRecord:private] => [_pk:CActiveRecord:private] => [_alias:CActiveRecord:private] => t [_errors:CModel:private] => Array ( ) [_validators:CModel:private] => [_scenario:CModel:private] => search [_e:CComponent:private] => [_m:CComponent:private] => )

Well I guess the statement:


if($this->project_targetdate)

is working.

I guess it just came down to not understanding the system and AJAX confusing me.

Thanks for your help