Skip to content

Incompatible with joins #44

@flipvrijn

Description

@flipvrijn

When the query builder is used in combination with joins, the parent_id (and probably others) column is ambiguous:

"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'parent_id' in where clause is ambiguous (SQL: select count(*) as aggregate from (select '1' as row from products left join shops on shops.id = products.shop_id left join categories on categories.id = products.category_id where parent_id is null) AS count_row_table)"

using the following query:

$products = \Product::whereIsRoot()
        ->leftJoin('shops', 'shops.id', '=', 'products.shop_id')
        ->leftJoin('categories', 'categories.id', '=', 'products.category_id')
        ->select(['products.id', 'shops.name as shop', 'products.image', 'products.brand', 'products.name',
            'products.unit', 'products.similarity', 'categories.name as category'
        ]);

For myself I quickly fixed it in QueryBuilder.php by altering the whereIsRoot() method to:

public function whereIsRoot()
{
    $this->query->whereNull($this->model->getTable().'.'.$this->model->getParentIdName());

    return $this;
}

But I think there are multiple places in the query builder that are incompatible with joins (still have to encounter them).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions