No I don't believe you're missing anything. I ran into a similar issue a while back and used getSelect()->where()
to work around it.
Starting with your query:
SELECT * FROM products WHERE (name like '%blah%' and name like
'%yes%') or (description like '%blah%' and description like '%yes%');
First, I created a collection and used the OR
array method to get the query.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addFieldToFilter(array(
array('attribute' => 'name', 'like' => '%blah%'),
array('attribute' => 'name', 'like' => '%yes%')
));
$collection->addFieldToFilter(array(
array('attribute' => 'description', 'like' => '%blah%'),
array('attribute' => 'description', 'like' => '%yes%')
));
$collection->load(true); // To dump the sql as text
This resulted in the following output:
SELECT `e`.*, IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS `name`, IF(at_description.value_id > 0, at_description.value, at_description_default.value) AS `description` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_entity_varchar` AS `at_name_default` ON (`at_name_default`.`entity_id` = `e`.`entity_id`) AND (`at_name_default`.`attribute_id` = '96') AND `at_name_default`.`store_id` = 0 LEFT JOIN `catalog_product_entity_varchar` AS `at_name` ON (`at_name`.`entity_id` = `e`.`entity_id`) AND (`at_name`.`attribute_id` = '96') AND (`at_name`.`store_id` = 1) INNER JOIN `catalog_product_entity_text` AS `at_description_default` ON (`at_description_default`.`entity_id` = `e`.`entity_id`) AND (`at_description_default`.`attribute_id` = '97') AND `at_description_default`.`store_id` = 0 LEFT JOIN `catalog_product_entity_text` AS `at_description` ON (`at_description`.`entity_id` = `e`.`entity_id`) AND (`at_description`.`attribute_id` = '97') AND (`at_description`.`store_id` = 1)
WHERE ((IF(at_name.value_id > 0, at_name.value, at_name_default.value) LIKE '%blah%') OR (IF(at_name.value_id > 0, at_name.value, at_name_default.value) LIKE '%yes%')) AND ((IF(at_description.value_id > 0, at_description.value, at_description_default.value) LIKE '%blah%') OR (IF(at_description.value_id > 0, at_description.value, at_description_default.value) LIKE '%yes%'))
You'll notice that the WHERE
statement is almost where you described it except the OR
and AND
are reversed.
I copied the WHERE
portion of the query and added an extra getSelect()->where()
line to the collection like this:
$collection = Mage::getModel('catalog/product')->getCollection();
// I added these lines to ensure that the collection would load
// the fields and do the necessary joins to pull the data
$collection->addFieldToFilter('name');
$collection->addFieldToFilter('description');
// I'm using the same WHERE statement but I have set the ANDs and ORs
// to match your requirements
$collection->getSelect()->where(" (IF(at_name.value_id > 0, at_name.value, at_name_default.value) LIKE '%blah%') AND (IF(at_name.value_id > 0, at_name.value, at_name_default.value) LIKE '%yes%')) OR ((IF(at_description.value_id > 0, at_description.value, at_description_default.value) LIKE '%blah%') AND (IF(at_description.value_id > 0, at_description.value, at_description_default.value) LIKE '%yes%')");
$collection->load(true); // To dump the sql as text again
The resulting output is:
SELECT `e`.*, IF(at_name.value_id > 0, at_name.value, at_name_default.value) AS `name`, IF(at_description.value_id > 0, at_description.value, at_description_default.value) AS `description` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_entity_varchar` AS `at_name_default` ON (`at_name_default`.`entity_id` = `e`.`entity_id`) AND (`at_name_default`.`attribute_id` = '96') AND `at_name_default`.`store_id` = 0 LEFT JOIN `catalog_product_entity_varchar` AS `at_name` ON (`at_name`.`entity_id` = `e`.`entity_id`) AND (`at_name`.`attribute_id` = '96') AND (`at_name`.`store_id` = 1) INNER JOIN `catalog_product_entity_text` AS `at_description_default` ON (`at_description_default`.`entity_id` = `e`.`entity_id`) AND (`at_description_default`.`attribute_id` = '97') AND `at_description_default`.`store_id` = 0 LEFT JOIN `catalog_product_entity_text` AS `at_description` ON (`at_description`.`entity_id` = `e`.`entity_id`) AND (`at_description`.`attribute_id` = '97') AND (`at_description`.`store_id` = 1)
WHERE (IF(at_name.value_id > 0, at_name.value, at_name_default.value) = '') AND (IF(at_description.value_id > 0, at_description.value, at_description_default.value) = '') AND ( (IF(at_name.value_id > 0, at_name.value, at_name_default.value) LIKE '%blah%') AND (IF(at_name.value_id > 0, at_name.value, at_name_default.value) LIKE '%yes%')) OR ((IF(at_description.value_id > 0, at_description.value, at_description_default.value) LIKE '%blah%') AND (IF(at_description.value_id > 0, at_description.value, at_description_default.value) LIKE '%yes%'))
The where()
in the getSelect()
could be shortened to:
(`name` like '%blah%' and `name` like '%yes%') or (`description` like '%blah%' and `description` like '%yes%');
Any other Magento people out there please feel free to correct me if I'm too far off-base with this or if you know of a way to accomplish the same goal without using this method.
hth!