Magento:合并eq,在addAttributeToFilter中为null [英] Magento: Combine eq and is null in addAttributeToFilter
问题描述
我想与OR组合为"is null"和"eq" => array()
I want to combine with OR "is null" and "eq" => array()
$collection->addAttributeToFilter('attributename', array('is' => null));
$collection->addAttributeToFilter('attributename', array(115,116));
但这不起作用.
推荐答案
要在OR
条件下使用addAttributeToFilter()
,可以传递如下数组:
To use addAttributeToFilter()
with an OR
condition you can pass an array of arrays like this:
$collection->addAttributeToFilter(
array(
array(
'attribute' => 'name_of_attribute_1',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'name_of_attribute_2',
'in' => array(115, 116)
)
)
);
乍一看,针对NULL
关键字进行过滤的定义可能看起来有些混乱,但是一旦您习惯了,它就非常简单.
The definition of filtering against NULL
keywords may look somewhat confusing at first glance, but it's plain simple, once you get used to it.
Magento支持string
类型的两种过滤器类型,即'null'
和'notnull'
,用于与NULL
关键字进行比较.
Magento supports two filter types of type string
, namely 'null'
and 'notnull'
, for comparisions against NULL
keywords.
请注意,即使您需要传递key => value对(因为使用了关联数组),使用过滤器类型'null'
或'notnull'
时,该值也始终会被忽略.
Be aware, that even though you need to pass key=>value pairs (because an associative array is used), the value will be always ignored, when filter type 'null'
or 'notnull'
is used.
var_dump(
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter(
array(
array(
'attribute' => 'description',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'sku',
'in' => array(115, 116)
)
)
)
->getSelectSql(true)
);
转储的输出可能会因您的商店配置(属性ID,商店数量等)而异,但WHERE
子句中的OR
逻辑应始终保持不变:
The output of the dump may vary depending on your shop configuration (attribute ids, count of stores, etc.), but the OR
logic in the WHERE
clause should always remain the same:
SELECT
`e`.*,
IFNULL(_table_description.value, _table_description_default.value) AS `description`
FROM
`catalog_product_entity` AS `e`
INNER JOIN
`catalog_product_entity_text` AS `_table_description_default` ON
(_table_description_default.entity_id = e.entity_id) AND
(_table_description_default.attribute_id='57') AND
_table_description_default.store_id=0
LEFT JOIN
`catalog_product_entity_text` AS `_table_description` ON
(_table_description.entity_id = e.entity_id) AND
(_table_description.attribute_id='57') AND
(_table_description.store_id='1')
WHERE (
(IFNULL(_table_description.value, _table_description_default.value) is NULL) OR
(e.sku in (115, 116))
)
这篇关于Magento:合并eq,在addAttributeToFilter中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!