【问题标题】:Search filter not working with custom search搜索过滤器不适用于自定义搜索
【发布时间】:2015-08-31 17:13:43
【问题描述】:

我的模型中有一个 search() 函数,为了使用一些自定义过滤器过滤我的结果,我对其进行了一些处理。所以在我的模型中我有这个:

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,               
                  ),
        ));
}

我的控制器如下所示:

  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,
        ));
}

我的问题是,当我使用 Yii 提供的搜索过滤器时,它们不起作用。我没有收到错误。他们不返回任何东西。如果我从 search() 中删除我添加的额外条件,那么过滤器就可以正常工作。但是我不能使用我的自定义过滤器。有人知道如何解决这个问题吗?在此先感谢!

【问题讨论】:

    标签: search yii


    【解决方案1】:

    没关系,我解决了。我将控制器更改为:

    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,
            ));
    

    【讨论】:

      猜你喜欢
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 2017-07-25
      相关资源
      最近更新 更多