Skip to main content
replaced http://magento.stackexchange.com/ with https://magento.stackexchange.com/
Source Link

If you are not afraid to build your own where clause (which I would not always suggest). Then what you could do is to use if statements in your where.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()
    ->where('if(entity_id = 16 AND type_id = "simple", TRUE, FALSE) = true')
    ->orWhere('if(entity_id = 17 AND type_id = "simple", TRUE, FALSE) = true')
    ->orWhere('if(entity_id = 18 AND type_id = "simple", TRUE, FALSE) = true');

One final note from me is that there looks like you could also use a mixture of the having function and addExpressionAttributeToSelect. This was taken from another stackexchange answer How to add dynamic field in magento collection?How to add dynamic field in magento collection?

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addExpressionAttributeToSelect('first_where', 'if({{entity_id}} = 16 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->addExpressionAttributeToSelect('second_where', 'if({{entity_id}} = 17 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->addExpressionAttributeToSelect('third_where', 'if({{entity_id}} = 18 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->getSelect()->having('first_where')->orHaving('second_where')->orHaving('third_where');

But using this I was getting the error Unknown column 'first_where' in 'having clause''

If you are not afraid to build your own where clause (which I would not always suggest). Then what you could do is to use if statements in your where.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()
    ->where('if(entity_id = 16 AND type_id = "simple", TRUE, FALSE) = true')
    ->orWhere('if(entity_id = 17 AND type_id = "simple", TRUE, FALSE) = true')
    ->orWhere('if(entity_id = 18 AND type_id = "simple", TRUE, FALSE) = true');

One final note from me is that there looks like you could also use a mixture of the having function and addExpressionAttributeToSelect. This was taken from another stackexchange answer How to add dynamic field in magento collection?

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addExpressionAttributeToSelect('first_where', 'if({{entity_id}} = 16 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->addExpressionAttributeToSelect('second_where', 'if({{entity_id}} = 17 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->addExpressionAttributeToSelect('third_where', 'if({{entity_id}} = 18 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->getSelect()->having('first_where')->orHaving('second_where')->orHaving('third_where');

But using this I was getting the error Unknown column 'first_where' in 'having clause''

If you are not afraid to build your own where clause (which I would not always suggest). Then what you could do is to use if statements in your where.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()
    ->where('if(entity_id = 16 AND type_id = "simple", TRUE, FALSE) = true')
    ->orWhere('if(entity_id = 17 AND type_id = "simple", TRUE, FALSE) = true')
    ->orWhere('if(entity_id = 18 AND type_id = "simple", TRUE, FALSE) = true');

One final note from me is that there looks like you could also use a mixture of the having function and addExpressionAttributeToSelect. This was taken from another stackexchange answer How to add dynamic field in magento collection?

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addExpressionAttributeToSelect('first_where', 'if({{entity_id}} = 16 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->addExpressionAttributeToSelect('second_where', 'if({{entity_id}} = 17 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->addExpressionAttributeToSelect('third_where', 'if({{entity_id}} = 18 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->getSelect()->having('first_where')->orHaving('second_where')->orHaving('third_where');

But using this I was getting the error Unknown column 'first_where' in 'having clause''

Source Link
David Manners
  • 27.3k
  • 9
  • 76
  • 220

If you are not afraid to build your own where clause (which I would not always suggest). Then what you could do is to use if statements in your where.

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()
    ->where('if(entity_id = 16 AND type_id = "simple", TRUE, FALSE) = true')
    ->orWhere('if(entity_id = 17 AND type_id = "simple", TRUE, FALSE) = true')
    ->orWhere('if(entity_id = 18 AND type_id = "simple", TRUE, FALSE) = true');

One final note from me is that there looks like you could also use a mixture of the having function and addExpressionAttributeToSelect. This was taken from another stackexchange answer How to add dynamic field in magento collection?

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addExpressionAttributeToSelect('first_where', 'if({{entity_id}} = 16 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->addExpressionAttributeToSelect('second_where', 'if({{entity_id}} = 17 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->addExpressionAttributeToSelect('third_where', 'if({{entity_id}} = 18 AND {{type_id}} = "simple", TRUE, FALSE)', array('entity_id', 'type_id'));
$collection->getSelect()->having('first_where')->orHaving('second_where')->orHaving('third_where');

But using this I was getting the error Unknown column 'first_where' in 'having clause''