Magento – Filtering results using LIKE

When you want to filter collection result by using like, you can use the following code:

$collection->addAttributeToFilter('name', array(
    array('like' => '% '.$needle.' %'), //spaces on each side
    array('like' => '% '.$needle), //space before and ends with $needle
    array('like' => $needle.' %') // starts with needle and space after
));

Passing the second parameter as an array of arrays will concatenate the conditions using OR.

 

Revisions

No comments yet.

Leave a Reply