1

您好在 Magento 中创建了一个自定义模型(资源基于 Mage_Core_Model_Resource_Db_Collection_Abstract )。一切都很好。我试图对表进行过滤,这将输出以下where子句:

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或其他东西来做到这一点的任何想法?

4

1 回答 1

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));
于 2012-06-28T23:56:24.350 回答