Thanks to visit codestin.com
Credit goes to github.com

Skip to content

join() comes before contain(), results in "Column not found" #17680

Open
@mehov

Description

@mehov

Description

What I have

return $this
    ->find('all')
    ->contain([
        'Categories' => [
            'Stores' => [
            ]
        ],
        'ProductsProducts' => [
            'RefProducts' => [
            ],
        ],
    ])
    ->join([
        [
            'alias' => 'RefCategories',
            'table' => 'Categories',
            'type' => 'LEFT',
            'conditions' => 'RefProducts.category_id = RefCategories.id'
        ],
    ])
;

What happens

In the query that CakePHP generates for that find, my manual join I defined last actually comes before the "contained" tables.

SELECT 
  *
FROM 
  Products Products 
  LEFT JOIN Categories RefCategories ON RefProducts.category_id = RefCategories.id 
  LEFT JOIN Categories Categories ON Categories.id = Products.category_id 
  INNER JOIN Stores Stores ON Stores.id = Categories.store_id 
  LEFT JOIN Products_Products ProductsProducts ON ProductsProducts.id = Products.id 
  INNER JOIN Products RefProducts ON RefProducts.id = ProductsProducts.ref_product_id 

As a result,

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'RefProducts.category_id' in 'on clause'

What I expected to happen

Because I defined the join() after contain(), I expected the contained tables to be available before the join kicks in.

If I copy the SQL from DebugKit and manually edit it to where the contained tables are joined first, the query works when I run it in Workbench

CakePHP Version

4.4.14

PHP Version

8.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions