Magento collection filtering 嵌套where条件的简单写法
只使用$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) ) );