2012-09-18 47 views
2

我無法在magento教程中找到此問題的解決方案。 如何在自定義模型中實現布爾OR運算符時涉及兩個屬性?官方教程中的示例僅演示了對一個字段sku使用OR布爾值。Magento上的複雜布爾條件

$filter_a = array('like'=>'a%'); 
$filter_b = array('like'=>'b%'); 

Mage::getModel('catalog/product') 
->getCollection() 
->addFieldToFilter('sku', array($filter_a, $filter_b)) 
->getSelect(); 

這相當於

WHERE e.sku like 'a%' or e.sku like 'b%' 

但是,如果我需要運行諸如條件是什麼:

WHERE (e.sku like 'a%' or e.sku like 'b%') or (table_price.value >= '10') 

我怎樣才能做到這一點上的Magento?由於

+0

使用OR過濾Magento集合的好資源:http://tweetorials.tumblr.com/post/10844018716/eav-collection-or-filters –

回答

2

你有你的語法incorrec,試試這個:

Mage::getModel('catalog/product') 
->getCollection() 
->addAttributeToFilter(
    array(
     array('attribute'=>'firstname', 'like'=>'test%'), 
     array('attribute'=>'lastname', 'like'=>'test%'), 
    ) 
) 

,你可以通過「SKU」或什麼都你想更換屬性名稱。每個數組條目將被OR'd。

+0

感謝Dan,Andrew。謝謝。 addAttributeToFilter和addFieldToFilter都可以工作。 – Edville