Yii2如何在活动记录中使用内置的mysql功能

时间:2018-09-24 05:23:27

标签: activerecord yii2

如何在Yii2活动记录中使用内置的MySQL函数? 这是我要隐藏在活动记录中的查询。

以及如何在SearchModel中实现相同功能?

SELECT * FROM `dts_track` where concat(date_received,time_received) != concat(date_released,time_released)

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以将字符串格式用于

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

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

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

    $query->andWhere('concat(date_received,time_received) != concat(date_released,time_released)');

    return $dataProvider;
}

答案 1 :(得分:-2)

在搜索功能中,让我为更好的生活做出贡献:

        if(isset($params['date_received']) AND isset($params['time_received'])){
            $datereceived = $params['date_received'].$params['time_received'];
        }

        if(isset($params['date_released']) AND isset($params['time_released'])){
            $datereleased = $params['date_released'].$params['time_released'];
        }

        $query->andWhere(':date1 != :date2', [':date1' => $datereceived , ':date2' => $datereleased ]);