条件,活动数据提供者,Yii 2中的CDbCriteria?

时间:2018-01-18 13:14:02

标签: php yii

我试图将此部分代码转移到yii2部分。它适用于Yii 1在控制器,但我已经读过,我不再需要Yii2中的标准,因为它不再存在。可以告诉我如何在yii2中运行它吗? 并且CDBCriteria在yii2中更好地用作ActiveQueryQuery

public function search()
    {
        $criteria=new CDbCriteria;
        $criteria->compare('id',$this->id);
        $criteria->compare('hashkey',$this->hashkey,true);
        $criteria->compare('ch',$this->ch,true);
        $criteria->compare('cimi',$this->cimi,true);
        $criteria->compare('dir',$this->dir,true);
        $criteria->compare('ourl',$this->ourl,true);
        $criteria->compare('imob',$this->imob,true);
        $criteria->compare('tStart',$this->tStart,true);
        $criteria->compare('tAnswer',$this->tAnswer,true);
        $criteria->compare('tEnd',$this->tEnd,true);
        $criteria->compare('state',$this->state,true);
        $criteria->compare('omob',$this->omob,true);
        $criteria->compare('date',$this->date,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }


    public function getInCalls($fromDate = false, $toDate = false) {
        $rawData = Yii::$app->db->createCommand ( "select id, date as Date, ch as Channel, dir as Direction, ourl as Twinkle, imob as IncomingCall, state as State from logs where tAnswer like '%+%' group by date order by date desc" )->queryAll ();

        return $rawData; 
    }


    public function getOutCalls($fromDate = false, $toDate = false) {
        $rawData = Yii::$app->db->createCommand ( "select id, date as Date, ch as Channel, dir as Direction, ourl as Twinkle, state as State, omob as OutgoingCall from logs where state like 'Mobile%' and (

omob like '07%' or omob like '+%') group by date order by date desc" )->queryAll ();

            return $rawData; 
        }

        public function getMissedCalls($fromDate = false, $toDate = false) {
            $rawData = Yii::$app->db->createCommand ( "select id, date as Date, ch as Channel, state as State, imob as Missed, ourl as Agent from logs where tEnd like '%+%' and imob is not null and state like '%Lan%' and tAnswer not like '%+%' and (imob like '07%' or imob like '+%') group by date order by date desc" )->queryAll ();
            return $rawData; 
        }

            return $rawData; 
        }


        public function getOutCalls($fromDate = false, $toDate = false) {
            $rawData = Yii::app ()->db->createCommand ( "select id, date as Date, ch as Channel, dir as Direction, ourl as Twinkle, state as State, omob as OutgoingCall from logs where state like 'Mobile%' and (omob like '07%' or omob like '+%') group by date order by date desc" )->queryAll ();

            return $rawData; 
        }

        public function getMissedCalls($fromDate = false, $toDate = false) {
            $rawData = Yii::app ()->db->createCommand ( "select id, date as Date, ch as Channel, state as State, imob as Missed, ourl as Agent from logs where tEnd like '%+%' and imob is not null and state like '%Lan%' and tAnswer not like '%+%' and (imob like '07%' or imob like '+%') group by date order by date desc" )->queryAll ();
            return $rawData; 
        }

由于

1 个答案:

答案 0 :(得分:1)

您可以使用gii生成的代码在modelSearch类中查看典型的过滤器查询(您应该使用此工具)

无论如何假设您有一个名为MyModel的模型,Yii2的tipycal搜索功能基于dataProvider和过滤条件,例如:

public function search($params)
{
    $query = MyModel::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
    ]);

    $query->andFilterWhere(['like', 'your_att1', $this->your_att1])
          ......
        ->andFilterWhere(['like', 'your_attN', $this->your_attN]);

    return $dataProvider;
}

在您的情况下,似乎所有列都不是字符串编号     公共功能搜索()     {

    $query = MyModel::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $query->andFilterWhere(['id' => $this->id ])
      ->andFilterWhere(['hashkey' => $this->hashkey ])
      ->andFilterWhere(['ch' => $this->ch ])
        .....
      ->andFilterWhere(['date' => $this->date ]);    

     return $dataProvider;
}