ASD
ASD

Reputation: 4957

YII Advance Search page

I have advance search page with all the fields declared as search variables. I am holding all the variables in the session.

All i want to know is on click of submit the view page fields will be saved in to session variables. So on grid pagination and sort using the session variables it will load the data.

But i want to clear the session on click of the link for advance search page? How to do that?

Controller.php

public function actionAdvance_search()
    {
        $modelAdvSearchFinder = new AdvSearchFinder;
        print_r($_GET);
        if(isset($_POST) && isset($_POST['AdvSearchFinder']['submit']))
        {
            $this->clearAdvSearchSessionVar();

            Yii::app()->session['id'] = ( $_POST['AdvSearchFinder']['id'])? Yii::app()->session['id'] = $_POST['AdvSearchFinder']['id']:"";
            Yii::app()->session['name'] = ( $_POST['AdvSearchFinder']['name'])? Yii::app()->session['name'] = $_POST['AdvSearchFinder']['name']:"";
            Yii::app()->session['age'] = ( $_POST['AdvSearchFinder']['age'])?Yii::app()->session['age'] = $_POST['AdvSearchFinder']['age']:"";
            Yii::app()->session['class'] = ( $_POST['AdvSearchFinder']['class'])? Yii::app()->session['class'] = $_POST['AdvSearchFinder']['class']:"";
            Yii::app()->session['section'] = ( $_POST['AdvSearchFinder']['section'])? Yii::app()->session['section'] = $_POST['AdvSearchFinder']['section']:"";
            Yii::app()->session['Email'] = ( $_POST['AdvSearchFinder']['Email'])? Yii::app()->session['Email'] = $_POST['AdvSearchFinder']['Email']:"";
            $modelAdvSearchFinder->attributes = $_POST['AdvSearchFinder'];

        }
        $id = Yii::app()->session['id'];
        $name = Yii::app()->session['name'];       
        $age = Yii::app()->session['age'];
        $class = Yii::app()->session['class'];  
        $section = Yii::app()->session['section'];  
        $Email = Yii::app()->session['Email'];    

        $criteria=new CDbCriteria(array('distinct'=>true,));          
        $criteria->compare('id',$id,true);
        $criteria->compare('name',$name,true);
        $criteria->compare('age',$age,true);
        $criteria->compare('class',$class,true);
        $criteria->compare('sectionPerson',$section,true);  
        $criteria->compare('Email',$Email,true);

        if($id != "" || $name != "" || $age  != "" || $class   != "" || $section    != "" || $Email      != "" || $TelePhone1  != "" || $TelePhone2  != "" || $Fax  != "" || $Address     != "" || $State       != "" || $Suburb      != "" || $PostCode   != "" || $Include_inactive != "F")
        {   
            $modelSearch= new CActiveDataProvider("tblClass", array(
                'criteria'=>$criteria,
            )); 
        }
        else
        {
            $modelSearch = "";
            $this->clearAdvSearchSessionVar();
        }
       $this->render('advance_search',array('modelSearch' => $modelSearch,'modelAdvSearchFinder'=>$modelAdvSearchFinder));
    }

model.php

class AdvSearchFinder extends CFormModel
{

    public $id;
    public $name;
    public $age;
    public $class;
    public $section;
    public $Email;

    public function rules()
    {
        return array(
            array('id,name,age,class,section,Email', 'safe'),
        );
    }    
    public function attributeLabels()
    {
        return array(
            'id'=>'ID',
            'name'=>'Name',
            'age'=>'AGE',
            'class'=>'Class',
            'section'=>'section',
            'Email'=>'Email'
        );
    }

}

Upvotes: 0

Views: 2791

Answers (5)

sarvesh
sarvesh

Reputation: 262

I am not getting why you are using session to store request variables because YII CActiveDataProvider already have option to set request parameters which can be use for sorting & pagination like follows:

$params=array('id'=>$_POST['AdvSearchFinder']['id'],'name'=>$_POST['AdvSearchFinder']['name'])
$dataProvider=new CActiveDataProvider($CmdbMaster,array(
                'criteria'=>$criteria,
                'pagination'=>array(
                        'params'=>$params,
                ),
                'sort'=>array(
                        'params'=>$params,
                ),

        ));

Upvotes: 0

Nikos Tsirakis
Nikos Tsirakis

Reputation: 739

You can create an onclick event with jquery that will clear this session like other users said:

unset(Yii::app()->session['var']);

Upvotes: 0

Orlymee
Orlymee

Reputation: 2357

unset(Yii::app()->session['var']);

Upvotes: 2

Pentium10
Pentium10

Reputation: 207912

I have two extensions that may help you to retain grid filter:

The remember filters extension adds up some functionality to the default possibilites of CActiveRecord/Model implementation.

It will detect the search scenario and it will save the filters from the GridView. This comes handy when you need to remember them between navigation during page changes. For lot of navigation and heavy filtering, this functionality can be activated by just a couple of lines.

It's pair is clear filters

Upvotes: 2

ernie
ernie

Reputation: 6356

Building on Orlymee's answer, you can wrap this in a check in your controller:

if(!Yii::app()->request->isAjaxRequest) { 
  unset(Yii::app()->session['var']);
}

Upvotes: 2

Related Questions