2
votes

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

4
have you tried this link stackoverflow.com/questions/5301231/… its working for me to give or condition in collection - Mufaddal
yes i have tried this one. But addAtrributeToFilter() function only works with EAV entities. It will not work for custom tables. - Abhijeet kumar sharma

4 Answers

5
votes

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'));
4
votes

In addition to Slayer Birden answer.

Also you can create more complicated queries like

    $rulesCollection
        ->addOrder('sort_order', Zend_Db_Select::SQL_ASC)
        ->addFieldToFilter(
            array('to_date', 'to_date'),
            array(array('gteq' => $now), array('null' => 'null')))
        ->addFieldToFilter(
            array('from_date', 'from_date'),
            array(array('lteq' => $now), array('null' => 'null')))
        ->addFieldToFilter('is_active', '1');

Constructed such SQL:

     SELECT `main_table`.* FROM `enterprise_targetrule` AS `main_table` 
     WHERE ((to_date >= '2013-03-21') OR (to_date IS NULL)) 
     AND ((from_date <= '2013-03-21') OR (from_date IS NULL)) 
     AND (is_active = '1') 
     ORDER BY sort_order ASC
2
votes
$collection->addFieldToFilter(array(
                array('attribute'=>'name', array('like' => '%'.$qry.'%')),
                array('attribute'=>'sku',  array('like' => '%'.$qry.'%'))));
for search by name and search by sku
0
votes

For $collections = Mage::getModel('sales/order')->getCollection() ->addAttributeToFilter('increment_id', array('in' => $sellerIncrementIds)) ->addAttributeToFilter('status', ['in' => ['pending','pending_seller_confirmation']]);