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([ … ]);
})
->toList();

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:
https://book.cakephp.org/3/en/orm/query-builder.html#adding-joins
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 = MeetingTasks.id',
                ],
            ],
            'Kanbans' => [
                'table' => 'kanbans',
                'type' => 'INNER',
                'conditions' => [
                    'MeetingTasks.kanban_id = Kanbans.id',
                ],
            ],
            'KanbanItems' => [
                'table' => 'kanban_items',
                'type' => 'INNER',
                'conditions' => [
                    'Kanbans.id = KanbanItems.kanban_id',
                ],
            ],
            'Tasks' => [
                'table' => 'tasks',
                'type' => 'INNER',
                'conditions' => [
                    'KanbanItems.task_id = Tasks.id',
                    'Tasks.status !=' => 6,
                    'Tasks.executed_by' => AppTable::$authUser->id,
                ],
            ]
        ])
      ->select($this)
      ->select($this->Tasks->Kanbans->KanbanItems->Tasks)->disableHydration()
      ->toList();

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 () {
   $this->loadModel('Tasks')->hasMany(....);
   $this->find()->matching(...)
}