2

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

  • have you tried this link http://stackoverflow.com/questions/5301231/addattributetofilter-and-or-condition-in-magentos-collection its working for me to give or condition in collection – Mufaddal Sep 25 '12 at 11:37
  • yes i have tried this one. But addAtrributeToFilter() function only works with EAV entities. It will not work for custom tables. – Abhijeet kumar sharma Sep 25 '12 at 18:47

4 Answers4

5

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'));
Slayer Birden
  • 3,664
  • 2
  • 22
  • 29
4

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
Community
  • 1
  • 1
Arkadij Kuzhel
  • 199
  • 2
  • 9
2
$collection->addFieldToFilter(array(
                array('attribute'=>'name', array('like' => '%'.$qry.'%')),
                array('attribute'=>'sku',  array('like' => '%'.$qry.'%'))));
for search by name and search by sku
0

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

Hassan Ali Shahzad
  • 2,439
  • 29
  • 32