如何使用集合过滤器实现以下目标?
SELECT * FROM products WHERE (name like '%blah%' and name like
'%yes%') or (description like '%blah%' and description like '%yes%');
或者出于某种原因这是不好的做法?
addFieldToFilter应用于集合执行AND,并且在每个集合中都可以实现OR,但这只允许:
(x OR y) AND (w OR z)
但不是
(x AND y) OR (w AND z)
我错过了什么吗?
答案 0 :(得分:4)
不,我不相信你错过任何东西。我不久前遇到了类似的问题并使用getSelect()->where()
来解决它。
从您的查询开始:
SELECT * FROM products WHERE (name like '%blah%' and name like
'%yes%') or (description like '%blah%' and description like '%yes%');
首先,我创建了一个集合,并使用OR
数组方法来获取查询。
$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
这导致以下输出:
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%'))
您会注意到WHERE
声明几乎就是您描述的地方,但OR
和AND
被撤消了。
我复制了查询的WHERE
部分,并在集合中添加了额外的getSelect()->where()
行,如下所示:
$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
结果输出为:
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%'))
where()
中的getSelect()
可以缩短为:
(`name` like '%blah%' and `name` like '%yes%') or (`description` like '%blah%' and `description` like '%yes%');
任何其他Magento人员请随时纠正我,如果我离这里太远,或者如果你知道如何在不使用这种方法的情况下实现同一目标。
HTH!