I managed to find simple examples of constructing a subquery, but I could not find a solution or find a solution when I need to include the WHERE clause. I am trying to simulate the following statement ...
SELECT ParentTable.*, (SELECT MAX(ChildTable.NumberField)
FROM ChildTable
WHERE ChildTable.FK_Id = ParentTable.Id)
FROM ParentTable
I think I need something like ...
$query = ParentClass::find()
->addSelect(
ChildClass::find()
->where('childTable.fk_id=parentTable.id')
->max('childTable.field1')
);
But this gives me an error: Column not found: 1054 Unknown column "parentTable.id" in the "where" section
EDIT: Including the names of real classes / tables ...
$endQuery = UnitSchedule::find()
->where('cm_courseschedule.id=cm_unitschedule.csch_id')
->max('cm_unitschedule.slot');
$query = CourseSchedule::find();
$query->addSelect($endQuery);
source
share