在多选属性上具有FIND_IN_SET的集合,但有些只有一个值


9

我正在建立一个产品集合,它将从multiselect属性中找到结果。

(将finset添加到集合中的相关代码-调整为显示真实ID值)

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'measurement');
    $value = array('finset' => array('237',
                '236',
                '235',
                '234',
                '233',));
    $collection->addAttributeToFilter($attribute, $value);

产生的sql(添加了可见性过滤器)是这样的:

SELECT DISTINCT
    e . *,
    at_measurement.value AS measurement,
    at_visibility.value AS visibility
FROM
    catalog_product_entity AS e
        INNER JOIN
    catalog_product_entity_varchar AS at_measurement ON (at_measurement.entity_id = e.entity_id) AND (at_measurement.attribute_id = '983') AND (at_measurement.store_id = 0)
        INNER JOIN
    catalog_product_entity_int AS at_visibility ON (at_visibility.entity_id = e.entity_id) AND (at_visibility.attribute_id = '526') AND (at_visibility.store_id = 0)
WHERE
    (e.attribute_set_id IN ('74')) AND (FIND_IN_SET('237',
            '236',
            '235',
            '234',
            '233',
            at_measurement.value)) AND (at_visibility.value IN ('2' , '4'))
GROUP BY e.entity_id

问题是我得到一个SQL错误:

"SQLSTATE[42000]: Syntax error or access violation: 1582 Incorrect parameter count in the call to native function 'FIND_IN_SET'"

并且我(认为)我明白为什么:一些多选值只有一个选项,因此没有逗号分隔的值可以使用FINSET

我是否正确为什么会产生此错误?如何编写此收集对象以解决此问题?

如果不是以上情况,我想念什么?

在mysql工作台中运行sql的结果减去find_in_set子句:

在查询编辑器中检查结果


Answers:


26

尝试将addAttributeToFilter与or条件一起使用

$collection->addAttributeToFilter($attribute,
    array(
        array('finset'=> array('237')),
        array('finset'=> array('238')),
        array('finset'=> array('239')),
    )
);

要么

$collection->addAttributeToFilter(
    array(
        array('attribute'=> 'attributecode','finset' => array('237')),
        array('attribute'=> 'attributecode','finset' => array('237')),
        array('attribute'=> 'attributecode','finset' => array('237')),
    )
);

1
我们如何不可以FIND_IN_SET?
Slimshadddyyy

如何使用“ AND”条件
Deeban Babu 2016年

1
对于“AND”,只是重复$收藏- > addAttributeToFilter()为每个值
沃特·
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.