您的位置:首页 > 其它

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

2015-09-01 20:23 344 查看
只使用$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)
)
);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: