Имеется две сущности: employee и department. Связь - many to many. У меня есть три таблицы: employee(id, name, salary), department(id, name) и department_employee(department_id, employee_id).
php код:
class Employee extends ActiveRecord
{
...
public function getDepartments()
{
return $this
->hasMany(Department::className(), ['id' => 'department_id'])
->viaTable('departments_employees', ['employee_id' => 'id']);
}
}
class Department extends ActiveRecord
{
...
public function getEmployees()
{
return $this
->hasMany(Employee::className(), ['id' => 'employee_id'])
->viaTable('departments_employees', ['department_id' => 'id']);
}
}
Задача: получить коллекцию с Department со всеми его значениями + максимальные значения salary из присоединенной таблицы. P.S. salary is integer.
UPD: Нужно реализовать такой запрос
"SELECT department.name, MAX(employee.salary), COUNT(employee.id) FROM `department`
INNER JOIN departments_employees
ON department.id = departments_employees.department_id
INNER JOIN employee
ON employee.id = departments_employees.employee_id
GROUP BY department.id;"