Magento addFieldToFilter:两个字段,匹配为 OR,而不是 AND

Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:两个字段,匹配为 OR,而不是 AND)
本文介绍了Magento addFieldToFilter:两个字段,匹配为 OR,而不是 AND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

过去几个小时我一直在坚持这个.我通过修改 /lib/Varien/Data/Collection/Db.php 中的几行来让它工作,但我更愿意使用正确的解决方案并保持我的核心不变.

I've been stuck on this for the last few hours. I got it working by hacking a few lines in /lib/Varien/Data/Collection/Db.php, but I'd rather use the proper solution and leave my core untouched.

我需要做的就是获取一个集合并按两个或多个字段对其进行过滤.比如说,customer_firstnameremote_ip.这是我的(没有破解 Db.php 的功能)代码:

All I need to do is get a collection and filter it by two or more fields. Say, customer_firstname and remote_ip. Here's my (disfunctional without hacking Db.php) code:

$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)

With a stock Db.php, I tried this: (sample taken from 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() 并给出这个错误:

If I wrap that with a try/catch, then it moves into _getConditionSql() and gives this error:

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

有没有人有任何可以执行此操作的有效代码?我正在运行 Magento 1.9(企业).谢谢!

Does anyone have any working, functional code for doing this? I'm running Magento 1.9 (Enterprise). Thanks!

推荐答案

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

I've got another way to add an or condition in the field:

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

这篇关于Magento addFieldToFilter:两个字段,匹配为 OR,而不是 AND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)