Magento:Combine eq在addAttributeToFilter中为空
我想与OR“is null”和“eq”=>array()结合使用Magento:Combine eq在addAttributeToFilter中为空,magento,Magento,我想与OR“is null”和“eq”=>array()结合使用 但这不起作用。要在或条件下使用addAttributeToFilter(),可以传递如下数组: $collection->addAttributeToFilter( array( array( 'attribute' => 'name_of_attribute_1', 'null' => 'this_value_doesnt_matter'
但这不起作用。要在
或条件下使用addAttributeToFilter()
,可以传递如下数组:
$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
关键字进行过滤的定义乍一看可能有点混乱,但一旦习惯了,它就非常简单了
Magento支持两种类型的过滤器类型string
,即'null'
和'notnull'
,用于与null
关键字进行比较
请注意,即使需要传递key=>值对(因为使用了关联数组),当使用过滤器类型'null'
或'notnull'
时,该值也将始终被忽略
例子
转储的输出可能因您的店铺配置(属性ID、店铺数量等)而异,但WHERE
子句中的或逻辑应始终保持不变:
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))
)
将addAttributeToFilter与OR条件和左联接一起使用
你可以这样做:
$collection->addAttributeToFilter(
array(
array('attribute' => 'name_of_attribute_1','null' => 'this_value_doesnt_matter'),
array('attribute' => 'name_of_attribute_2','in' => array(115, 116))
)
'',
'left'
);
- 我的产品属性
已过期
带有Yes和No值,我只希望那些产品没有值。下面的查询工作为我提供了获取具有neq 1且不为null的记录的功能
- 将不等于和空条件与或组合
$collection->addAttributeToFilter('attribue_name', array('or'=> array(
0 => array( 'neq' => '1'),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
- 您可以使用
eq
更改neq
对不起,我想要的不是两个不同的属性。。。您的示例的结果是:_属性_1的name_为NULL,并且(_属性_2的name_=115或_属性_2的name_=116),但是我想要:(_属性_1的name_为NULL或_属性_1的name_=115或_属性_1的name_=116)。这在你的代码中是不可能的。@timopeschka:奇怪,对我来说它工作得很完美。我添加了一个示例及其输出。执行我的示例时,您的WHERE
子句看起来如何?您使用的是哪种类型的集合?有些,比如扁平的销售表,有自己版本的addAttributeToFilter
方法,因为它们实际上不是EAV集合。这也许可以解释你看到的差异。在找到你的帖子之前,我都快疯了。太多了!虽然这段代码可以回答这个问题,但提供关于它如何以及为什么解决这个问题的附加上下文将提高答案的长期价值。
$collection->addAttributeToFilter(
array(
array('attribute' => 'name_of_attribute_1','null' => 'this_value_doesnt_matter'),
array('attribute' => 'name_of_attribute_2','in' => array(115, 116))
)
'',
'left'
);
$collection->addAttributeToFilter('attribue_name', array('or'=> array(
0 => array( 'neq' => '1'),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');