Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add OR condition in Magento Collection

I want to fetch data from table by applying OR condition in Collection

I am using these lines of code

$collect  = Mage::getModel('storelocater/storelocater')->getCollection()->addFieldToFilter(array(
                array(
'attribute' => 'country', 'eq' => 'india'),
array(
'attribute' => 'state', 'eq' => 'up')
)); echo $data = $collect->getSelect();

it prints the output

SELECT main_table.* FROM storelocater AS main_table WHERE ((Array = '') OR (Array = ''))

i have also used addAttributeToFilter instead of addFieldToFilter but it returns fatal error

like image 606
Abhijeet kumar sharma Avatar asked Nov 30 '25 15:11

Abhijeet kumar sharma


1 Answers

addAttributeToFilter function will work only with EAV entities (like Customers, Products, Categories etc.). addFieldToFilter has slightly different syntax for OR operator:

$collect  = Mage::getModel('storelocater/storelocater')
    ->getCollection()->addFieldToFilter(array('country','state'), array('india','up'));
like image 173
Slayer Birden Avatar answered Dec 03 '25 08:12

Slayer Birden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!