在Presta的查询中添加新的JOIN

时间:2013-06-27 12:34:39

标签: prestashop

我有来自Category.php类(getProducts函数)的代码:

$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, MAX(product_attribute_shop.id_product_attribute) id_product_attribute, product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, pl.`description`, pl.`description_short`, pl.`available_now`,
            pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, MAX(image_shop.`id_image`) id_image,
            il.`legend`, m.`name` AS manufacturer_name, cl.`name` AS category_default,
            DATEDIFF(product_shop.`date_add`, DATE_SUB(NOW(),
            INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).'
                DAY)) > 0 AS new, product_shop.price AS orderprice
        FROM `'._DB_PREFIX_.'category_product` cp
        LEFT JOIN `'._DB_PREFIX_.'product` p
            ON p.`id_product` = cp.`id_product`
        '.Shop::addSqlAssociation('product', 'p').'
        LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa
        ON (p.`id_product` = pa.`id_product`)
        '.Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1').'
        '.Product::sqlStock('p', 'product_attribute_shop', false, $context->shop).'
        LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
            ON (product_shop.`id_category_default` = cl.`id_category`
            AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
        LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
            ON (p.`id_product` = pl.`id_product`
            AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
        LEFT JOIN `'._DB_PREFIX_.'image` i
            ON (i.`id_product` = p.`id_product`)'.
        Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
        LEFT JOIN `'._DB_PREFIX_.'image_lang` il
            ON (image_shop.`id_image` = il.`id_image`
            AND il.`id_lang` = '.(int)$id_lang.')
        LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
            ON m.`id_manufacturer` = p.`id_manufacturer`
        WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
            AND cp.`id_category` = '.(int)$this->id
            .($active ? ' AND product_shop.`active` = 1' : '')
            .($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
            .($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '')
            .' GROUP BY product_shop.id_product';

我想添加新的LEFT JOIN,所以我刚刚添加了

        LEFT JOIN `'._DB_PREFIX_.'attribute` atr
            ON (pa.`id_attribute` = atr.`id_attribute`)

在查询结束时。但它现在什么也没有回报。我做错了什么?

1 个答案:

答案 0 :(得分:2)

您在product_attribute的SQL表别名中犯了错误:

LEFT JOIN `'._DB_PREFIX_.'attribute` atr ON (product_attribute.`id_attribute` = atr.`id_attribute`)

这是正确的语法:

LEFT JOIN `'._DB_PREFIX_.'attribute` atr ON (pa.`id_attribute` = atr.`id_attribute`)

要发现此错误,最好的方法是在执行查询之前显示查询并对其进行测试(例如使用phpMyAdmin)。

我希望这可以解决。