searchModel中的Yii2列别名

时间:2017-02-24 14:38:27

标签: php mysql alias

我遇到别名问题,尝试使用来自2个表的最小值的别名获取ActiveDataProvider。

$query->select('karta_przyrzadu.*'); 
        $query->joinWith('dzialania');
        $query->addSelect(['dataVar1' => '(NOW() + interval karta_przyrzadu.co_zostalo day)']);
        $query->addSelect(['dzialania.data_plan as `dataVar2`']);
        $query->addSelect(['least(dataVar1, dataVar2) as nextActionDate']);

声明模型中的Vars,并在rules()中标记为安全:

public $nextActionDate;
public $dateRangeStart;
public $dateRangeEnd;
public $dataVar1;
public $dataVar2;

过滤

if (!is_null($this->nextActionDate) && strpos($this->nextActionDate, ' - ') !== false ) {
            list($dateRangeStart, $dateRangeEnd) = explode(' - ', $this->nextActionDate);
            $query->andFilterWhere(['between', 'dataVar2', $dateRangeStart, $dateRangeEnd]);
        }

不是因为:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'dataVar1' in 'field list'

正在执行的SQL是:SELECT COUNT()FROM(SELECT karta_przyrzadu,(NOW()+ interval co_zostalo day)AS dataVar1,{{1} } .data_plan为dzialania,less(dataVar1,dataVar2)为nextActionDate FROM dataVar2 LEFT JOIN karta_przyrzadu ON dzialaniakarta_przyrzadu = id_przyrzdzialania在哪里(id_przyrz_d BETWEEN' 2017-02-18' AND' 2017-02-24')和(不是nextActionDate =& #39; Wycofany'))GROUP BY statusid_przyrz

任何人都可以帮助我?

0 个答案:

没有答案