Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to contain() same table multiple times using different aliases #17679

Open
mehov opened this issue May 9, 2024 · 0 comments
Open

Unable to contain() same table multiple times using different aliases #17679

mehov opened this issue May 9, 2024 · 0 comments
Milestone

Comments

@mehov
Copy link
Contributor

mehov commented May 9, 2024

Description

Say I have a join table where I map products to products, for example two counterparts of each other. (Please assume it makes sense.)

Then I want to select a pair of products out of that table. Each product belongsTo category, and finally each category belongsTo store.

In the join table where the product pairs are, I can define the association twice:

    $this->belongsTo('Products', [
        'foreignKey' => 'product_id',
        'joinType' => 'INNER',
    ]);
    $this->belongsTo('RefProducts', [
        'className' => 'Products',
        'foreignKey' => 'ref_product_id',
        'joinType' => 'INNER',
    ]);

But when I find() a product, attempting to contain its counterpart and its own containments, I don't get both sets of contained information. I'm assuming this is because the contained joins use the same aliases. I tried to overwrite the aliases for each individual contain, but it didn't work. My first attempt was the most obvious:

->contain([
    'Categories' => [
        'Stores' => [
        ]
    ],
    'ProductsProducts' => [
        'RefProducts' => [
            'Categories' => [
                'alias' => 'RefCategories',
                'Stores' => [
                    'className' => 'RefStores',
                ]
            ],
        ],
    ],
])

That resulted in The alias association is not defined. I tried a few more and then finally found a complete list of all accepted options:

protected array $_containOptions = [
'associations' => 1,
'foreignKey' => 1,
'conditions' => 1,
'fields' => 1,
'sort' => 1,
'matching' => 1,
'queryBuilder' => 1,
'finder' => 1,
'joinType' => 1,
'strategy' => 1,
'negateMatch' => 1,
];

Unless I'm missing something, there doesn't seem to be a way to manually set an alias for an individual contain?

CakePHP Version

4.4.14

PHP Version

8.3.1

@mehov mehov added the defect label May 9, 2024
@markstory markstory modified the milestones: 4.6.0, 4.5.6 May 9, 2024
@markstory markstory added the ORM label May 9, 2024
@markstory markstory modified the milestones: 4.5.6, 4.5.7 Jun 23, 2024
@markstory markstory modified the milestones: 4.5.7, 4.5.8 Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants