# SQL query witch contain, alias table name

**URL:** <https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401>\
**Category:** Need Help\
**Created:** [March 5, 2020, 8:46am UTC](https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401 "2020-03-05T08:46:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [March 5, 2020, 8:46am UTC](https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401/1 "2020-03-05T08:46:57Z")

</div>

I have query with contain parameter:

```
            'contain' => [
            'ProductsItemsVariants' => [
                'sort' => ['ProductsItemsVariants.cena' => 'ASC']
            ]

```

But result is object array field =\> products\_items\_variants (array)

How do I make a field named after a table alias? eg.: ProductsItemsVariants ??

Thank you

---

<div class="post-metadata">

**Author:** ![gludie](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@gludie](https://discourse.cakephp.org/u/gludie)\
**Post date:** [March 5, 2020, 9:46am UTC](https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401/2 "2020-03-05T09:46:20Z")

</div>

Have you tried using additional options, like alias ?

`'contain' => ['ProductsItemsVariants' => ['alias' => 'xyz', 'sort' => ['ProductsItemsVariants.cena' => 'ASC'] ]`

I couldn’t try yet, but may be it works ?

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [March 5, 2020, 9:52am UTC](https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401/3 "2020-03-05T09:52:55Z")

</div>

I tried, and message is: The alias association is not defined on ProductsItemsVariants.  
Where and how do I need to define an alias?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 5, 2020, 3:37pm UTC](https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401/4 "2020-03-05T15:37:10Z")

</div>

It’s not clear what you’re looking for. Can you show an example of what you get, and what you want it to look like? Also, what version of Cake are you using?

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [March 5, 2020, 4:10pm UTC](https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401/5 "2020-03-05T16:10:00Z")

</div>

Here is query:

```
        return [
        'fields' => [
            'ProductsItems.id',
            'ProductsItems.aktivni',
            'ProductsItemsTexts.nazev',
            'ProductsItemsTexts.popis',
            'ProductsItemsTexts.popis_velikosti'
        ],
        'join' => [
            'ProductsItemsTexts' => [
                'table' => 'products_items_texts',
                'type' => 'LEFT',
                'conditions' => [
                    'ProductsItemsTexts.products_item_id = ProductsItems.id',
                ],
            ],
        ],
        'conditions' => [
            'ProductsItems.aktivni =' => 1,
            'ProductsItemsTexts.slug =' => $slug,
            'ProductsItemsTexts.language_id =' => Configure::read('LanguageSet.id'),
        ],
        'contain' => [
            'ProductsItemsVariants' => [
                'sort' => ['ProductsItemsVariants.cena' => 'ASC']
            ]
        ]
    ];

```

I have version 4.0.2

And result is

 ![cake-array](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/4/46ccd6d47cab28178979edc80f23e1699775dcc1.png)

I need the variable to be: ProductsItemsVariants not products\_items\_variants

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 5, 2020, 4:30pm UTC](https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401/6 "2020-03-05T16:30:23Z")

</div>

If this is just because it suits your personal preference better, my suggestion would be to try to put that aside and work with Cake’s standards. This is how it names things by default, and while working against those defaults is certainly possible, it tends to add to your time because code that it bakes, code you find in tutorials, answers you’ll get online, all will assume the defaults and you have to fiddle with them to get to whatever you changed yours to.

That said, if you really want to change this, use the `propertyName` setting when you create the [association](https://book.cakephp.org/4/en/orm/associations.html). There are, after all, situations where you really do need to do this.

---

<div class="post-metadata">

**Author:** ![shadowx.jb](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@shadowx.jb](https://discourse.cakephp.org/u/shadowx.jb)\
**Post date:** [March 5, 2020, 4:32pm UTC](https://discourse.cakephp.org/t/sql-query-witch-contain-alias-table-name/7401/7 "2020-03-05T16:32:57Z")

</div>

Thank you very much, it works!
