Magento addFieldToFilter: Two fields, match as OR, not AND

后端 未结 10 1239
孤街浪徒
孤街浪徒 2020-12-04 09:42

I\'ve been stuck on this for the last few hours. I got it working by hacking a few lines in /lib/Varien/Data/Collection/Db.php, but I\'d rather use the proper s

10条回答
  •  醉酒成梦
    2020-12-04 10:19

    To filter by multiple attributes use something like:

    //for AND
        $collection = Mage::getModel('sales/order')->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('my_field1', 'my_value1')
        ->addFieldToFilter('my_field2', 'my_value2');
    
        echo $collection->getSelect()->__toString();
    
    //for OR - please note 'attribute' is the key name and must remain the same, only replace //the value (my_field1, my_field2) with your attribute name
    
    
        $collection = Mage::getModel('sales/order')->getCollection()
            ->addAttributeToSelect('*')
            ->addFieldToFilter(
                array(
                    array('attribute'=>'my_field1','eq'=>'my_value1'),
                    array('attribute'=>'my_field2', 'eq'=>'my_value2')
                )
            );
    

    For more information check: http://docs.magentocommerce.com/Varien/Varien_Data/Varien_Data_Collection_Db.html#_getConditionSql

提交回复
热议问题