3

I have a collection that I have to filter according to multiple strings in one field. The trick is that one of the possible values in that field needs to be picked only if another field's value meets a certain condition. For example:

$this->addFieldToFilter(
  'my_field1', 
  array(
    'val1', |
    'val2', |  <--- This is the OR part
    'val3', |
    'val4 AND my_field2 like 'my_field2_val' // <-- This is the tricky part
   )
);

Can any of you guys help me? PS: My collection is a flat one not EAV.

1 Answers1

4

Works only for EAV collections - I think.

You can add ANDs and ORs like this:

OR

$collection->addAttributeToFilter(
    array(
        array('attribute'=> 'someattribute','like' => 'value'),
        array('attribute'=> 'otherattribute','like' => 'value'),
        array('attribute'=> 'anotherattribute','like' => 'value'),
    )
);

AND inside OR

$collection->addAttributeToFilter(
    array(
        array( // AND
            array('attribute'=> 'someattribute','like' => 'value'),
            array('attribute'=> 'otherattribute','like' => 'value'),
        ),
        array('attribute'=> 'anotherattribute','like' => 'value'),
    )
);

So just stack the arrays.

Fabian Blechschmidt
  • 35,388
  • 8
  • 75
  • 182