SQL query witch contain, alias table name

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

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 ?

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

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?

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

I need the variable to be: ProductsItemsVariants not products_items_variants

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. There are, after all, situations where you really do need to do this.

2 Likes

Thank you very much, it works!