Fix ambiguous column error on shop page JOIN query

Prefixed is_active, category, product_type_id, name, description, and ORDER BY columns with table alias p to resolve ambiguity with the product_types JOIN.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-14 19:56:47 +00:00
parent e6ca96b75c
commit 5c0927af39
+11 -5
View File
@@ -33,13 +33,19 @@ if ($search) {
}
$whereClause = implode(' AND ', $where);
// Prefix columns that are ambiguous in the JOIN query
$joinWhereClause = str_replace(
['is_active = 1', 'category =', 'product_type_id =', '(name LIKE', 'description LIKE'],
['p.is_active = 1', 'p.category =', 'p.product_type_id =', '(p.name LIKE', 'p.description LIKE'],
$whereClause
);
// Sort
$orderBy = match($sort) {
'price_low' => 'COALESCE(sale_price, price) ASC',
'price_high' => 'COALESCE(sale_price, price) DESC',
'name' => 'name ASC',
default => 'created_at DESC'
'price_low' => 'COALESCE(p.sale_price, p.price) ASC',
'price_high' => 'COALESCE(p.sale_price, p.price) DESC',
'name' => 'p.name ASC',
default => 'p.created_at DESC'
};
// Get total count
@@ -48,7 +54,7 @@ $pagination = paginate($totalProducts, $page, 12);
// Get products
$products = db()->fetchAll(
"SELECT p.*, pt.name AS type_name, pt.type_id AS type_slug FROM products p LEFT JOIN product_types pt ON p.product_type_id = pt.type_id WHERE {$whereClause} ORDER BY {$orderBy} LIMIT :limit OFFSET :offset",
"SELECT p.*, pt.name AS type_name, pt.type_id AS type_slug FROM products p LEFT JOIN product_types pt ON p.product_type_id = pt.type_id WHERE {$joinWhereClause} ORDER BY {$orderBy} LIMIT :limit OFFSET :offset",
array_merge($params, ['limit' => $pagination['per_page'], 'offset' => $pagination['offset']])
);