0
votes

Hi I have a magento site that uses categories as giftlists for people. Each category has a list_holder_1 and list_holder_2 attribute.

I've created a form that users can search for giflists (categories) with a textbox for each attribute.

I'm using the getModell..getCollection() method with addAttributeToFilter with like causes but I'm getting odd results.

Here is my code..

<?php $list_holder_1 = $_POST['list_holder_1']; ?>
<?php $list_holder_2 = $_POST['list_holder_2']; ?>
<?php $_collection = Mage::getModel('catalog/category')->getCollection();
$_collection->addAttributeToSelect('*');
$_collection->addAttributeToFilter(
            array(
                array('attribute'=>'list_holder_1', 'like'=>'%'.$list_holder_1.'%'),
                array('attribute'=>'list_holder_1', 'like'=>'%'.$list_holder_2.'%'),
            ));
$_collection->addAttributeToFilter(
            array(
                array('attribute'=>'list_holder_2', 'like'=>'%'.$list_holder_1.'%'),
                array('attribute'=>'list_holder_2', 'like'=>'%'.$list_holder_2.'%'),
            ));
echo $_collection->getSelect();
?>

The user should be able to search for each name in either textbox. So my query ends up with a where clause like this:

I have only added one name is one box - 'Hill'

WHERE (e.entity_type_id = '3') AND ((_table_list_holder_1.value like '%Hill%') OR (_table_list_holder_1.value like '%%')) AND ((_table_list_holder_2.value like '%Hill%') OR (_table_list_holder_2.value like '%%'))

As you can see it's checking for a blank value in both fields. And for some reason it's pulling out every category. Even though only 1 has the list_holder_1 value of 'Hill'.

Anyone any clue as to where I've gone wrong?

Regards, Billy

1

1 Answers

2
votes

% is a wildcard so '%%' is matching all values. If you want to match exact values don't use like and %, use eq instead.