Here is a quick tip to add OR query and AND query to collection object. I am implementing this on product collection.
Getting product collection
// PRODUCT COLLECTION
$collection = Mage::getModel('catalog/product')->getCollection();
Adding AND query condition
Filtering collection to select only those products whose sku is like ‘ch’ AND status is equal to 1 (i.e. enabled status).
// AND QUERY
$collection->addAttributeToFilter('sku', array('like' => '%ch%'));
$collection->addAttributeToFilter('status', array('eq' => '1'));
Adding OR query condition for addAttributeToFilter
Filtering collection to select only those products whose sku is like ‘ch’ OR status is equal to 1 (i.e. enabled status).
// OR QUERY
$collection->addAttributeToFilter(array(
array(
'attribute' => 'sku',
'like' => '%ch%'),
array(
'attribute' => 'status',
'eq' => '1')
));
Adding OR query condition for addFieldToFilter
// Adding OR condition to the query
$collection->addFieldToFilter(
array('sku', 'id'), // columns/field of database table
array( // conditions
array( // conditions for 'sku' (first field)
array('in' => array('text_1', 'text_2', 'text_3')),
array('like' => '%text')
),
array('gt' => 5) // condition for 'id' (second field)
)
);
You may check the query statement with the following code
echo $collection->printLogQuery(true);
OR, with the following code:
echo $collection->getSelect();
Thanks.