Note that there are some explanatory texts on larger screens.

plurals
  1. POMagento addFieldToFilter: Two fields, match as OR, not AND
    text
    copied!<p>I've been stuck on this for the last few hours. I got it working by hacking a few lines in <code>/lib/Varien/Data/Collection/Db.php</code>, but I'd rather use the proper solution and leave my core untouched.</p> <p>All I need to do is get a collection and filter it by two or more fields. Say, <code>customer_firstname</code> and <code>remote_ip</code>. Here's my (disfunctional without hacking <code>Db.php</code>) code:</p> <pre><code>$collection = Mage::getModel('sales/order')-&gt;getCollection()-&gt; addAttributeToSelect("*")-&gt; addFieldToFilter(array(array('remote_ip', array('eq'=&gt;'127.0.0.1')), array('customer_firstname', array('eq'=&gt;'gabe'))), array('eq'=&gt;array(1,2,3))); </code></pre> <p>With a stock <code>Db.php</code>, I tried this: (sample taken from <a href="http://magentoexpert.blogspot.com/2009/12/retrieve-products-with-specific.html" rel="noreferrer">http://magentoexpert.blogspot.com/2009/12/retrieve-products-with-specific.html</a>)</p> <pre><code>$collection-&gt;addFieldToFilter(array( array('name'=&gt;'orig_price','eq'=&gt;'Widget A'), array('name'=&gt;'orig_price','eq'=&gt;'Widget B'), )); </code></pre> <p>But that gives me this error:</p> <pre><code>Warning: Illegal offset type in isset or empty in magento/lib/Varien/Data/Collection/Db.php on line 369 </code></pre> <p>If I wrap that with a try/catch, then it moves into _getConditionSql() and gives this error:</p> <pre><code>Warning: Invalid argument supplied for foreach() in magento/lib/Varien/Data/Collection/Db.php on line 412 </code></pre> <p>Does anyone have any working, functional code for doing this? I'm running Magento 1.9 (Enterprise). Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload