# Alias for Matching Statement

**URL:** <https://discourse.cakephp.org/t/alias-for-matching-statement/10184>\
**Category:** Need Help\
**Created:** [February 24, 2022, 9:34am UTC](https://discourse.cakephp.org/t/alias-for-matching-statement/10184 "2022-02-24T09:34:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Drea58](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@Drea58](https://discourse.cakephp.org/u/Drea58)\
**Post date:** [February 24, 2022, 9:34am UTC](https://discourse.cakephp.org/t/alias-for-matching-statement/10184/1 "2022-02-24T09:34:39Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [February 24, 2022, 11:01am UTC](https://discourse.cakephp.org/t/alias-for-matching-statement/10184/2 "2022-02-24T11:01:21Z")

</div>

Try with this:  
[https://book.cakephp.org/3/en/orm/query-builder.html#adding-joins](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.

---

<div class="post-metadata">

**Author:** ![Drea58](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@Drea58](https://discourse.cakephp.org/u/Drea58)\
**Post date:** [February 24, 2022, 11:10am UTC](https://discourse.cakephp.org/t/alias-for-matching-statement/10184/3 "2022-02-24T11:10:58Z")

</div>

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.

```auto
$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();

```

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [February 24, 2022, 11:20am UTC](https://discourse.cakephp.org/t/alias-for-matching-statement/10184/4 "2022-02-24T11:20:11Z")

</div>

> [@Drea58](#):
>
> `->select($this->Tasks->Kanbans->KanbanItems->Tasks)`

I think I just learn something from you 🙂 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.

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [February 24, 2022, 2:05pm UTC](https://discourse.cakephp.org/t/alias-for-matching-statement/10184/5 "2022-02-24T14:05:27Z")

</div>

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](https://discourse.cakephp.org/t/sql-error-when-loading-multiple-associations-with-the-same-name/9817/3)

---

<div class="post-metadata">

**Author:** ![Drea58](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@Drea58](https://discourse.cakephp.org/u/Drea58)\
**Post date:** [February 24, 2022, 2:26pm UTC](https://discourse.cakephp.org/t/alias-for-matching-statement/10184/6 "2022-02-24T14:26:05Z")

</div>

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 ? 🤔

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [February 24, 2022, 6:54pm UTC](https://discourse.cakephp.org/t/alias-for-matching-statement/10184/7 "2022-02-24T18:54:46Z")

</div>

> [@Drea58](#):
>
> Would not make sense for the model to have it named ModStTasks2 just for one query to work.

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”)

> [@Drea58](#):
>
> Maybe add a relation on the fly ? 🤔

yes like this

```php
// in a controller
public function tasks () {
   $this->loadModel('Tasks')->hasMany(....);
   $this->find()->matching(...)
}

```
