問題描述
過去幾個小時我一直在堅持這個.我通過修改 /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_firstname
和 remote_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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!