R
R
Roman Savitsky2017-09-05 19:08:50
Yii
Roman Savitsky, 2017-09-05 19:08:50

What am I doing wrong when selecting rows for the current month in Yii2?

Good afternoon !
With such a SQL query, right in phpMyAdmin, my line is

SELECT * FROM `brk_image_post_upload`
WHERE date > LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
  AND date < DATE_ADD(LAST_DAY(CURDATE()), INTERVAL 1 DAY) AND juzer_id = 10;

Trying to repeat the same example in Yii2:
$juzer_photo = $this->find()->where(['juzer_id' => 10])
                       ->andWhere(['>', 'date', 'LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH'])
                       ->andWhere(['<', 'date', 'DATE_ADD(LAST_DAY(CURDATE()), INTERVAL 1 DAY)'])
                       ->one();

This option doesn't help either.
//->andWhere('date > :date1',[':date1' => 'LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH'])
//->andWhere('date < :date2',[':date2' => 'DATE_ADD(LAST_DAY(CURDATE()), INTERVAL 1 DAY)'])

As a result, it does not find anything, it is empty.
What is the magic?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-09-05
@PRC

1 thing you need to understand is in what format the date is in the database. If in the form of a date, not a number, then mysql like this:
And in yii2 using AR, it seems like this (but worth checking):

SomeModel::find()->andWhere("MONTH(`date`) = MONTH(NOW()) AND YEAR(`date`) = YEAR(NOW())")->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question