M
M
Maxim2018-10-11 22:17:08
Yii
Maxim, 2018-10-11 22:17:08

Yii Sampling from base on dates. What's wrong?

Hello! There are three
tables: appointment , profile , event ( appointments , profile, events) 2018-11-25 (duration 1 day) Format: Dates in the database are stored in Ymd format I need to check for free user dates. The free date is determined by the event (table event). When assigning a user (appointment table) to an event, we select all appointments of the current user, except for the current event, and perform a search
5bbfa07f992fc599546429.png





for free dates according to the table with events (table event). If we find an appointment, event date range, or event date that matches/is included in the current event dates, we throw an error that the user is already busy on those dates.
I do it with this query:

$appointmentsInDates = Appointment::find()
                ->joinWith(['event', 'profile'])
                ->andWhere([Profile::tableName() . '.user_id' => $model->certification->user_id])
                ->andWhere(['!=', Event::tableName() . '.id', $model->event_id])
                ->andWhere(['>=', Event::tableName() . '.date_from', '2018-11-25'])
                ->andWhere(['<=', Event::tableName() . '.date_to', '2018-11-25'])
                ->groupBy(Event::tableName() . '.id')
                ->exists();

SQL
SELECT `appointment`.* FROM `appointment` 
LEFT JOIN `event` ON `appointment`.`event_id` = `event`.`id`
LEFT JOIN `certification` ON `appointment`.`judge_comitet_id` = `certification`.`id` 
LEFT JOIN `profile` ON `certification`.`user_id` = `profile`.`user_id` 
WHERE (`profile`.`user_id`=139) 
AND (`event`.`id` != 55) 
AND (`event`.`date_from` >= 2018-11-25) 
AND (`event`.`date_to` <= 2018-11-25) 
GROUP BY `event`.`id` 
ORDER BY `event`.`date_to`

This request only works if the event to which the assignment is made has a wider date range than the events to which the user was also assigned...
However, when assigned to Event 2 , which takes place on the same day from 2018-11 -25 to 2018-11-25 and has a date range less than the events to which the user was also assigned - does not find any appointments. Although this appointment exists for Event 1 , which takes place over two days from 2018-11-24 to 2018-11-25 with a wider range of dates.
How can I query the user's free dates so that he takes into account all events and checks all date ranges, even those that are outside the scope of the current event?
Thanks to!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-10-11
@slo_nik

If your date is stored as 2018-11-24 , then how can you compare them?
It turns out something like this
As far as I understand in any way.
I would add time to the comparison
But this may not work either, since the date in the database has no time.
Add time, even preferably in timestamp
Then you can compare like this:
Here is Event::tableName() . '.date_from', timestamp, this also produces timestamp strtotime('2018-11-24' . '00:00:00')
And the second date can be compared like this:
Something like this, if I'm not mistaken of course.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question