Alias for Matching Statement

I want to make a query like this:
$meetings = $this->find()->matching(‘Tasks.Kanbans.KanbanItems.Tasks’, function ($q) {
return $q->where([ … ]);

As you can see, I have “Tasks” twice in the path.
I only need the Meetings and the second Tasks Model in the result.
But $meeting->_matchingData only contains one Tasks Result.
The result for the second Tasks Model gets overridden by the first Tasks Model in the path.
I didn’t find a way to set an alias for one of the “Tasks”.
How can I set an alias or get the result in another way ?

Try with this:
you can define aliases there, and probably you must do the rest manually, like selecting every one field that you are interested with.

I did this, but hoped for a better solution.
It gets very huge and ugly.
And it feels wrong to manually define all the tables and join conditions that are already available in the Model definitions.
Now I collect the values myself in an extra loop at the end after the query.

$meetings = $this->find()->join([
            'MeetingTasks' => [
                'table' => 'tasks',
                'type' => 'INNER',
                'conditions' => [
                    'Meetings.task_id =',
            'Kanbans' => [
                'table' => 'kanbans',
                'type' => 'INNER',
                'conditions' => [
                    'MeetingTasks.kanban_id =',
            'KanbanItems' => [
                'table' => 'kanban_items',
                'type' => 'INNER',
                'conditions' => [
                    ' = KanbanItems.kanban_id',
            'Tasks' => [
                'table' => 'tasks',
                'type' => 'INNER',
                'conditions' => [
                    'KanbanItems.task_id =',
                    'Tasks.status !=' => 6,
                    'Tasks.executed_by' => AppTable::$authUser->id,

I think I just learn something from you :slight_smile: anyway you can select values from joined tables, so maybe you don’t need loop? But I many times also did some extra work in PHP and quit thinking about super SQL queries if there is not many data.

If you have multiple relations with the same tables, you can set a new name to separate both usages.

see this reply: SQL error when loading multiple associations with the same name

Those are two different Models, and both have a relation to the same Tasks Model.
Meetings hasOne Tasks
KanbanItems hasOne Tasks

Would not make sense for the model to have it named ModStTasks2 just for one query to work.
Maybe add a relation on the fly ? :thinking:

Why not? It just for one usage. I have it for making reports for example. (e.g. multiple tables linking to users as “created_by”)

yes like this

// in a controller
public function tasks () {