Answer the question
In order to leave comments, you need to log in
Yii2 AR Join + Where, how?
There are two tables Employee and Schedule, schedule is related to employee through schedule.employee = employee.id
We need to select all employee schedules for a specific day.
In SQL the query looks like this:
select * from employee
left join schedule on schedule.employee = employee.id
where schedule.date = <ДАТА>
$qEmployee = Employee::find()->joinWith('schedules')->where(['schedule.date'=>$curDate]);
$Employee = $qEmployee->all();
Answer the question
In order to leave comments, you need to log in
read here https://github.com/yiisoft/yii2/blob/master/docs/g... , it's a pity there is no translation yet, can you do it for me?
$qEmployee = Employee::find()->joinWith('schedules')->where(['schedule.date'=>$curDate]);
here you choose all employees!!!!! which have an entry in the schedule table for the date $curDate. Those. you made a filter on a selection from the employee table. And when you access employee->schedule everything is displayed!!! records from the schedule table for this employee.
In order to do what you want, you need to impose conditions on a selection from the schedule table. I will just give an example
$employers = Employee::find()->joinWith(['schedules'=>function ($query) use($curDate) {
$query->where(['date'=>$curDate]);
}])->all();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question