addAttributeToFilter中的多条件(OR和AND)


19

如何在addAttributeToFilter中创建多条件?

我想要这样的SQL查询结果(附有图像):

WHERE ((`e`.`news_from_date` > '2013-09-12') OR (`e`.`news_to_date` < '2013-09-12'))
AND ((((`e`.`special_price` IS NULL))) OR (((`e`.`special_price` IS NOT NULL)) AND ((`e`.`special_from_date` < '2013-09-12') OR (`e`.`special_to_date` > '2013-09-12'))))

$collection->addAttributeToFilter('special_price', array('null'=>'special_price'), 'left');

谢谢在此处输入图片说明


我发现这非常有用:blog.chapagain.com.np/...
nicolallias

Answers:


41

您应该能够结合大多数这些技术来创建所需的查询。对于销售表,您可能会使用addFieldToFilter-但Zend_Db_Expr可能对您而言阻力最小的路径是:

addAttributeToFilter:

根据Magento Wiki:创建具有OR条件的括号时,您可以执行以下操作:

如果传递了数组但未指定属性代码,则它将被解释为一组OR条件,将以相同的方式进行处理。

因此,由此我们可以构造以下内容:

$collection->addAttributeToFilter(
    array(
        array('attribute'=> 'someattribute','like' => 'value'),
        array('attribute'=> 'otherattribute','like' => 'value'),
        array('attribute'=> 'anotherattribute','like' => 'value'),
    )
);

这将输出WHERE以下格式的子句:

WHERE ((someattribute LIKE 'value') OR (otherattribute LIKE 'value') OR (anotherattribute LIKE 'value'))

addFieldToFilter:

如果模型直接链接到数据库表,则需要按以下步骤通过名称将条件应用于数据库列:

$collection->addFieldToFilter(
    array('title', 'content'),
    array(
        array('like'=>'%$titlesearchtext%'), 
        array('like'=>'%$contentsearchtext%')
    )
)

Zend_Db_Expr:

对于更复杂的构造,您可以使用构建自己的where子句Zend_Db_Expr。例如 :

$collection->getSelect()->where(new Zend_Db_Expr("(e.created_at > '2013-01-01 00:00:00' OR e.created_at <'2012-01-01 00:00:00)"));

资源 :

/programming/5301231/addattributetofilter-and-or-condition-in-magentos-collection

/programming/3826474/magento-addfieldtofilter-two-fields-match-as-or-not-and/7851884#7851884


感谢您的答复,但我想做的条件是:WHERE(CONDITION_1 OR(CONDITION_2 AND(CONDITION_3 OR CONDITION_4)))。我已经在上面发布了我想要的查询。
Andhi Irawan

1
我以为我指出了可以使用的观点Zend_Db_Expr$collection->getSelect()->where(new Zend_Db_Expr("(CONDITION_1 OR (CONDITION_2 AND (CONDITION_3 OR CONDITION_4)))"));
philwinkle 2013年

谢谢philwinkle.base在您的回答上,这是我使用addAttributeToFilter和AND和OR条件所做的示例:pastebin.com/ZznxLAai
Andhi Irawan 2014年

有没有办法确定哪个属性产生结果?我正在跨三个字段(name,,)搜索自定义模型集合altnernate_namedescription并想知道何时由于该description属性而产生结果。
pspahn

4

如果您要包括与组合

$colemanManufacturerSku = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('sku')
    ->addAttributeToSelect('sku_1')
    ->addAttributeToSelect('sku_2')
    ->addAttributeToSelect('sku_3')
    ->addAttributeToFilter('type_id', array('eq' => 'simple'))
    // Above condition will AND with following OR
    ->addAttributeToFilter(
        array(
            array(
                'attribute' => 'sku_1',
                'neq' => ''
            ),
            array(
                'attribute' => 'sku_2',
                'neq' => ''
            ),
            array(
                'attribute' => 'sku_3',
                'neq' => ''
            )
        )
    );

2
为您的解决方案+1,但我需要完全相反,例如WHERE(CONDITION_1和CONDITION_2)或(CONDITION_3 AND CONDITION_4))),能否请您帮忙
Haris

1

或使用addFieldToFilter条件


$collection->addFieldToFilter
                (   
                    array('email','mobile'),
                    array($emaiId,$mobNum)
                );

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.