thr
thr

Reputation: 33

Search filter not working with custom search

I have a search() function in my model and I have messed around a bit with it in order to filter my results with some custom filters. So in my model I have this:

public function search()
    {

        // @todo Please modify the following code to remove attributes that should not be searched.
        $startdate='';
        $enddate='';
        if ($this->year!=''){
        $year=explode('-', $this->year);
        $date=DateTime::createFromFormat('Y', $year[0])->format('d/m/Y');
        $startdate = General::getSeasonStartDate($date);
        $enddate = General::getSeasonEndDate($date);
        } 
        $criteria=new CDbCriteria;

        $criteria->with=array(
            'contracts'=>array(
            'select'=>'contracts.contractdate',
            'together'=>true
        ),

            'schoolstudents' => array(
                    'together' => true,
                    'select' => false,
                ),
              'schoolstudents.school'

            );
        //$criteria->order='lastname, firstname, fathername, mothername';
        if (Yii::app()->user->CompanyID){
           $criteria->compare('school.companyid',Yii::app()->user->CompanyID);
        } 
        if(Yii::app()->user->SchoolID){
            $criteria->compare('schoolstudents.schoolid',Yii::app()->user->SchoolID);
        }
        $criteria->compare('schoolstudents.schoolid', $this->schoolid);
        //$criteria->compare('studentid',$this->studentid);
        $criteria->compare('lastname',$this->lastname,true);
        $criteria->compare('firstname',$this->firstname,true);
        $criteria->compare('fathername',$this->fathername,true);


        $criteria->compare('telephone1',$this->telephone1,true);
        $criteria->compare('telephone2',$this->telephone2,true);
        $criteria->compare('cellphone1',$this->cellphone1,true);
        $criteria->compare('cellphone2',$this->cellphone2,true);
        $criteria->compare('email1',$this->email1,true);
        $criteria->compare('email2',$this->email2,true);
      if($this->year!=''){ 
       if ($startdate && $enddate){

             $from = DateTime::createFromFormat('d/m/Y', $startdate)->format('Y-m-d');
             $to = DateTime::createFromFormat('d/m/Y', $enddate)->format('Y-m-d');

             if ($this->filter=='R'){
                $criteria->addBetweenCondition('contractdate',$from, $to, 'AND');
             }
             else {
                $criteria->addBetweenCondition('schoolstudents.createddate',$from, $to, 'AND');
             } 
          } 
      } else {
          if ($this->filter=='R'){
           $criteria->addCondition('contracts.studentid');
          } else {
               $criteria->addCondition('schoolstudents.studentid');
          }

      }

        if(isset($this->birthdate))
        {
            if($this->birthdate!='') {
                $criteria->addCondition('year(birthdate)=:birthdate');
                $criteria->params=CMap::mergeArray($criteria->params,array(
                                      ':birthdate'=>$this->birthdate,
                                      )
                        );
            }
        }

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>'lastname asc',
            ),
            'pagination'=>array(
                'pageSize'=>50,               
                  ),
        ));
}

my controller looks like this:

  public function actionAdmin()
    { 
        $model=new Student('search');
        $model->unsetAttributes();
            $y=date('Y');
            $y1=date('Y',strtotime($y.'+1 year'));
            $test=$y.'-'.$y1;

            $model->year=$test;
            $model->filter='A';
        if (isset($_GET['Student']['year'])){
            $model->year=($_GET['Student']['year']);

        }
        if (isset($_GET['Student']['filter'])){
            $model->filter=$_GET['Student']['filter'];
        } 

        if(isset($_GET['Student']))
                $model->attributes=$_GET['Student'];

        $this->render('admin',array(
                'model'=>$model,
        ));
}

and my problem is that when I use the search filters provided by Yii they don't work. I don't get an error. They don't return anything. If I remove from search() the extra conditions I've added then the filters work fine. But then I can't use my custom filters. Anybody has any idea how to solve this?Thanks in advance!

Upvotes: 0

Views: 145

Answers (1)

thr
thr

Reputation: 33

Never mind I solved it. I changed my controller to this:

public function actionAdmin()
{ 
        $model=new Student('search');
        $model->unsetAttributes();
        $y=date('Y');
        $y1=date('Y',strtotime($y.'+1 year'));
        $test=$y.'-'.$y1;    

        if (isset($_GET['Student']['year'])){
            $model->year=$_GET['Student']['year'];

        }
        if (isset($_GET['Student']['filter'])){
            $model->filter=$_GET['Student']['filter'];
        } 

        if(isset($_GET['Student'])){
                $model->attributes=$_GET['Student'];
        } else{
            //for custom & ajax filters to work together
            $model->year=$test;
            $model->filter='A';
        }
        $this->render('admin',array(
                'model'=>$model,
        ));

Upvotes: 0

Related Questions