mirror of
https://github.com/myronblair/tomsjavajive
synced 2026-06-30 17:50:32 -05:00
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:
@@ -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']])
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user