I created a query with an EXISTS subquery, which works fine:
$query = (new Builder())
->from(['a' => Activity::class])
->columns(['id', 'name', 'DATE_FORMAT(booked_start, "%Y") AS year'])
->where('NOT EXISTS(SELECT e.id FROM ' . Event::class . ' e WHERE e.activity_id = a.id)');
But now when I try to add an order:
$query = (new Builder())
->from(['a' => Activity::class])
->columns(['id', 'name', 'DATE_FORMAT(booked_start, "%Y") AS year'])
->where('NOT EXISTS(SELECT e.id FROM ' . Event::class . ' e WHERE e.activity_id = a.id)')
->orderBy('year ASC');
I get this error: Column 'year' doesn't belong to any of the selected models (1), when preparing: ...
If I remove the subquery:
$query = (new Builder())
->from(['a' => Activity::class])
->columns(['id', 'name', 'DATE_FORMAT(booked_start, "%Y") AS year'])
->orderBy('year ASC');
It does work
Is this a bug?