M
M
Maxim2018-10-23 21:49:43
Yii
Maxim, 2018-10-23 21:49:43

Why does Yii2 SQL query execute, but not via ActiveRecord?

Hello.
Why does query 1 execute and 2 return an empty set?
Request 1:

Yii::$app->db->createCommand('SELECT MONTH(reg_date) AS Months, COUNT(*) AS RegCount FROM users WHERE YEAR(reg_date) = YEAR(NOW()) ORDER BY Months')->queryAll();

Request 2:
$this->find()->select(['MONTH(reg_date) AS Months', 'COUNT(*) AS RegCount'])->where(['YEAR(reg_date)' => 'YEAR(NOW())'])->groupBy('Months')->all();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-10-23
@webinar

I think it's about escaping when parsing select, try doing it through yii\db\Expression:
https://www.yiiframework.com/doc/api/2.0/yii-db-ex...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question