FrenkR
FrenkR

Reputation: 323

Magento AND/OR combination model filter

Hi created a custom model inside Magento(resource is based on Mage_Core_Model_Resource_Db_Collection_Abstract ). Everything works pretty much fine. I tried to make a filter on table, that would output the following where clause:

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))

Any idea on how to do that using addFilter or something?

Upvotes: 1

Views: 378

Answers (1)

nachito
nachito

Reputation: 7035

The addFieldToFilter/addAttributeToFilter methods are not well suited for a query that complex. You will have to construct your query by hand:

$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));

Upvotes: 2

Related Questions