码迷,mamicode.com
首页 > 其他好文 > 详细

Magento collection filtering 嵌套where条件的简单写法

时间:2015-09-01 21:33:24      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

只使用$collection->addFieldToFilter(),不使用addAttributeToFilter()或者Zend_Db_Expr(可用于更复杂的where语句)
例如: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)"));
 
AND关系
------------------------------------------------------
min_weight < $weight AND
max_weight >= $weight 
 
$_collection->addFieldToFilter(‘min_weight‘, array(‘lt‘ => $weight))
->addFieldToFilter(‘max_weight‘, array(‘gteq‘ => $weight));

 

 
OR关系
------------------------------------------------------
min_weight > $weight OR
$weight > max_weight
 
$_collection->addFieldToFilter(
array(‘min_weight‘,‘max_weight‘),
    array(
        array(‘gt‘ => $weight),
        array(‘lt‘ => $weight)
    )
);    

 

 
AND和OR嵌套关系
------------------------------------------------------
min_weight > $weight AND
(
$weight <= max_weight AND max_weight<>NULL
OR
max_weight IS NULL
)
 
$_collection->addFieldToFilter(‘min_weight‘, array(‘lt‘ => $weight))
->addFieldToFilter(
    array(‘max_weight‘,‘max_weight‘),
    array(
        array(‘gteq‘ => $weight, ‘notnull‘ => true),
        array(‘null‘ => true)
    )
);            

 

Magento collection filtering 嵌套where条件的简单写法

标签:

原文地址:http://www.cnblogs.com/kogen/p/4776786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!