Pass filter to default search (on model) gererated by gii

Hi all.

How can I pass a filter value to the admin view???

I want for example pass the role=admin for the admin view.

On model:





	public function search()

	{

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

		// should not be searched.

		$criteria=new CDbCriteria;

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

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

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

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

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

		$criteria->compare('role', $this->role, false);

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

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

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

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

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

		return new CActiveDataProvider(get_class($this),array(

			'pagination'=>array(

				'pageSize'=>S::app()->user->getState('pageSize', APP_DEFAULT_PAGE_SIZE),

			),

			'criteria'=>$criteria,

		));

	}




on Controller





	public function actionAdmin()

	{

	

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

			S::app()->user->setState('pageSize',(int)$_GET['pageSize']);

			unset($_GET['pageSize']);  //would interfere with pager and repetitive page size change

		}

		$model=new Users('search');

		//clear any default values

		$model->unsetAttributes();

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

			$model->attributes=$_GET['Users'];

		}

		$this->render('admin',array(

			'model'=>$model,

		));

	}



If I am understanding your question correctly. You can access $_GET parameters from the search() method to change your CDbCriteria.

Alternatively you can modify your search() method to take parameters and pass the filter options in where you are using it.

yes. i want access the get param. could you give me on example?

GET parameters can be accessed like the following: $_GET[‘filter’], in 1.1.4 you can take advantage of binding which looks much cleaner. http://www.yiiframework.com/doc/guide/basics.controller#action-parameter-binding

Here is how I solve this in a few applications. You could potentially make it more advanced and allow multiple filters.




      switch($filter)

      {

        case self::$FILTER_UNASSIGNED:

          $this->status = self::$STATUS_NEW;

          break;


        case self::$FILTER_SCHEDULED:

          $this->status = '>' . self::$STATUS_SCHEDULED;

          break;


        case self::$FILTER_NOTSCHEDULED:

          $this->status = '<' . self::$STATUS_SCHEDULED;

          break;


        case self::$FILTER_SUBMITTER:

          $this->submitterId = Yii::app()->user->id;

          break;


        case self::$FILTER_DUE:

          $this->dateAssigned = '<=' . date('Y-m-d');

          $this->status = '<' . self::$STATUS_COMPLETE;

          break;


        case self::$FILTER_AWAITING_VERIFY:

          $this->status = self::$STATUS_AWAITING_VERIFY;

          break;


        default:

          break;

      }

If you want to just set the role to ‘admin’ for the adminView you can set this in the controller after unsetAttributes() like




     public function actionAdmin()

     {

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

                     S::app()->user->setState('pageSize',(int)$_GET['pageSize']);

                     unset($_GET['pageSize']);

             }

             $model=new Users('search');

             $model->unsetAttributes();

             $model->role='admin';            // <----

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

                     $model->attributes=$_GET['Users'];

             }

             $this->render('admin',array(

                     'model'=>$model,

             ));

     }

This way by default $model->role is ‘admin’… and if there is a $_GET[‘role’] parameter sent, $model->role will get that value (if it’s one of the safe attributes)

Hey. Thanks! Worked!