Magento Collection Query with AND / OR Operators

For those who are new with magento, below post will help them to understand that how AND & OR operator should work with Magento to fetch Collection from Database

Please Note:
A. I am assuming that you are aware about Magento Model.
B. In below Examples, lets considered  $salesorderid =9;


Ex:1 - Magento SQL Query with - OR Operator

$outstandingCollection= Mage::getModel('adminhtmlmodule/adminhtmlmodule')->getCollection()->addFieldToSelect('salesorderid');
                $outstandingCollection->addFieldToFilter(array('salesorderid','paystatus'), array(
                  array('salesorderid','eq'=>$salesorderid),
                  array('paystatus','eq'=>'0')
             )
         );

Output :
SELECT `main_table`.`salesorderid` FROM `extracharges` AS `main_table` WHERE ((salesorderid = '9') OR (paystatus = '0'))

---
Ex:2 - Magento SQL Query with - AND Operator 

$outstandingCollection= Mage::getModel('adminhtmlmodule/adminhtmlmodule')->getCollection()
             ->addFieldToSelect('salesorderid');
                $outstandingCollection->addFieldToFilter('salesorderid','9');
                $outstandingCollection->addFieldToFilter('paystatus','0');



Output:
SELECT `main_table`.`salesorderid` FROM `extracharges` AS `main_table` WHERE (salesorderid = '9') AND (paystatus = '0')

Comments

Popular posts from this blog

Add/Remove Product from Wishlist using Ajax in Magento

Terraform Basics - To get Started

Send Promo Code to New Registered Customer in Magento