php - Magento addFieldToFilter : Two fields, 匹配为 OR,而不是 AND

标签 php magento magento-1.9

在过去的几个小时里,我一直被困在这个问题上。我通过修改 /lib/Varien/Data/Collection/Db.php 中的几行代码来实现它,但我宁愿使用正确的解决方案,并且不影响我的核心。

我需要做的就是获取一个集合并按两个或多个字段对其进行过滤。比如,customer_firstnameremote_ip。这是我的(在没有破解 Db.php 的情况下无法正常工作)代码:

$collection = Mage::getModel('sales/order')->getCollection()->
addAttributeToSelect("*")->
addFieldToFilter(array(array('remote_ip', array('eq'=>'127.0.0.1')),
array('customer_firstname', array('eq'=>'gabe'))), array('eq'=>array(1,2,3)));

使用股票 Db.php,我尝试了这个:(样本取自 http://magentoexpert.blogspot.com/2009/12/retrieve-products-with-specific.html)

$collection->addFieldToFilter(array(
    array('name'=>'orig_price','eq'=>'Widget A'),
    array('name'=>'orig_price','eq'=>'Widget B'),           
));

但这给了我这个错误:

Warning: Illegal offset type in isset or empty  in magento/lib/Varien/Data/Collection/Db.php on line 369

如果我用 try/catch 包装它,那么它会移动到 _getConditionSql() 并给出这个错误:

Warning: Invalid argument supplied for foreach()  in magento/lib/Varien/Data/Collection/Db.php on line 412

有没有人有任何工作的、功能性的代码来做这件事?我正在运行 Magento 1.9(企业版)。谢谢!

最佳答案

我还有另一种方法可以在字段中添加 or 条件:

->addFieldToFilter(
    array('title', 'content'),
    array(
        array('like'=>'%$titlesearchtext%'), 
        array('like'=>'%$contentsearchtext%')
    )
)

关于php - Magento addFieldToFilter : Two fields, 匹配为 OR,而不是 AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3826474/

相关文章:

php - Magento - 以编程方式从 url 添加图像?

java - 使用 php 和 JSON 将 android 连接到 mySQL

php - 使用 PHP 解析 JSON 文件

php - 包括特殊字符在内的任何英文 ASCII 字符的正则表达式

Magento:默认 Adminhtml Controller 中的 PHP 警告

apache - 从 IPv4 地址重定向到 127.0.0.1

php - 如何在 PHP 中使用准备好的语句将数组结果插入到 mysql 中

php - 如何在 magento 中替换 5000 种产品的 sku 编号

php - 我可以为不同的类别设置不同的颜色吗

MySQL 2006 - Magento 索引管理