2012-06-28 75 views
1

您好在Magento內部創建了一個自定義模型(資源基於Mage_Core_Model_Resource_Db_Collection_Abstract)。一切工作都很好。 我試圖讓桌子上的過濾器,這將輸出以下其中子句:Magento AND/OR組合模型過濾器

where 
    product_id = 1 
    and ((customer_id = 0 and customergroup_id = 2) or (customergroup_id = 0 and customer_id = 3)) 
    and ((productgroup_id = 0 and product_class = 8) or (product_class = 0 and productgroup_id = 4)) 

如何,使用addFilter或事做任何想法?

回答

2

addFieldToFilter/addAttributeToFilter方法不太適合查詢複雜。您將不得不手動構建您的查詢:

$collection->getSelect() 
    ->where('product_id = ?', 1) 
    ->where(sprintf(
     '((customer_id = %d AND customergroup_id = %d) OR (customer_id = %d AND customergroup_id = %d))', 
     0, 2, 3, 0)) 
    ->where(sprintf(
     '((productgroup_id = %d AND product_class = %d) OR (productgroup_id = %d AND product_class = %d))', 
     0, 8, 4, 0));