duandang2123 2013-01-02 12:34
浏览 41

Magento集合 - 使用addAttributeToFilter嵌套where子句

I am trying to add multiple filters to a magento collection which I can do on a basic level. What I really want to do is to be able to filter a collection by a number of nested filters which could be any level deep. For example i know I can add mutiple filters using code like:

$collection->addAttributeToFilter('example_attribute', array('eq' => 1));

What I want to be able to do is to have filters (AND & OR) that can be nested within each other. I am not sure if Magento collection allow you to do this already but I have not been able to find any examples online.

To give further detail and example: I would need to filter a collection where the equivalent SQL (ignore the selected fields I need and the JOINS) would be:

SELECT .........

WHERE 

(attribute1 = 1
OR 
atttribute2 = 'yes')

AND
qty > 5

AND(
  (attribute3 != 'no'
  AND 
  attribute4 = 50 )
  OR
  (attribute3 != 'no'
  AND 
  attribute6 > 50 )
  )

This is just a quick crude example but the basic problem is how to nest any number of filters.

  • 写回答

1条回答 默认 最新

  • drpogkqqi536984960 2013-01-02 13:59
    关注

    You can use OR conditioning using code like this:

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

    Edit: Using this you should be able to do at least AND and OR conditioning in a query. It seems that the standard behavior does not allow for a nested AND OR conditioning. So I assume you would need to write a custom query for that. For further reference, check the file Mage_Eav_Model_Entity_Collection_Abstract in which the method addAttributeToFilter is defined.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度