搜索过滤器无法使用自定义搜索

时间:2015-08-31 17:13:44

标签: search yii

我的模型中有一个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()中删除我添加的额外条件,那么过滤器工作正常。但后来我无法使用自定义过滤器。任何人都知道如何解决这个问题?提前谢谢!

1 个答案:

答案 0 :(得分:0)

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

service {
  “sap.test::myTable”
    create forbidden
    update forbidden
    delete forbidden;
}