Collections – Filter Multiple Multi-Select Attribute Values

attributescollection;MySQL

That's not a typo.

I am aware that I need to use 'finset' to filter my multi-select attributes; however, I am trying to filter multiple values at once and getting:

Incorrect parameter count in the call to native function 'FIND_IN_SET.

Here's some sample code:

foreach ($options as $option) {
    // $option[0] contains an attribute ID as a string
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($option[0]);

    if ($attribute->getFrontendInput() == 'multiselect') {
        $collection->addAttributeToFilter($attribute->getAttributeCode(), array('finset' => $option[1]));
    } else {
        $collection->addAttributeToFilter($attribute->getAttributeCode(), array('in' => $option[1]));
    }
}

What I have on the frontend is a set of fields, each set corresponds to a specific attribute and contains checkboxes for each attribute value. Based on these submissions, the collection should filter out what has been selected.

Everything works great except for the single case where I attempt to filter two of the multi-select options at the same time. If I only choose one of them, the search works properly. If I choose two or more, I get the MySQL error mentioned above.

Any ideas? Or am I forced to use custom SQL statements to build this filter?

Best Answer

From what I understand you want to send 2 or more values and filter by them using OR.
Something like this:

...
WHERE
    FIND_IN_SET($v1, `some_field`) OR
    FIND_IN_SET($v2, `some_field`) OR
...

$v1 & $v2 are the values of your checkboxes

For that you need to pass to the addAttributeToFilter and array of arrays.

->addAttributeToFilter(
    array(
          array('attribute'=>'some_attribute', 'finset'=>$v1),
          array('attribute'=>'some_attribute', 'finset'=>$v2),
    )
);

Here is a possible implementation.

$data = THE ARRAY OF THE CHECKBOX VALUES;

$filter = array();
foreach ($data as $value) {
    $filter[] = array(
        'attribute' => $attribute->getAttributeCode(),
        'finset'    => $value
    );
}

if (count($filter) > 0) {
    $collection->addAttributeToFilter($filter);
}