Magento和/或组合模型过滤器

时间:2012-06-28 22:33:25

标签: mysql magento orm

您好在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 或其他方法做到这一点的想法吗?

1 个答案:

答案 0 :(得分: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));