magentomagento-1.7

How to add OR condition in Magento collection for created_at and updated_at date?


I have following code

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('category_ids')
            ->addAttributeToSelect('manufacturer')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('special_price')
            ->addAttributeToSelect('regularprice')          
            ->addAttributeToSelect('tier_price_for_bundle') 
            ->addAttributeToSelect('special_from_date') 
            ->addAttributeToSelect('special_to_date')   
            ->addAttributeToFilter('type_id', array('in' => array('simple')))
            ->addAttributeToFilter('updated_at', array('from'=>date("Y-m-d", time()-86400)));           
            ->addAttributeToFilter('created_at', array('from'=>date("Y-m-d", time()-86400)));

I want to add OR condition for updated_at and created_at in following

->addAttributeToFilter('updated_at', array('from'=>date("Y-m-d", time()-86400)));           
->addAttributeToFilter('created_at', array('from'=>date("Y-m-d", time()-86400)));

So that whether a product is created or updated my collection should load.

How can I do this?


Solution

  • To add an OR filter for ONE attribute on EAV collections: the attribute code is the first argument and the conditions are the second.

    $col->addAttributeToFilter(‘name’, array(array(‘like’ => ‘M%’), array(‘like’ => ‘%O’))); (get items whose name starts with M OR ends with O)
    

    To add an OR filter for DIFFERENT attributes on EAV collections: only pass one argument, an array of conditions with attributes.

    $col->addAttributeToFilter(array(array(‘attribute’ => ‘weight’, ‘in’ => array(1,3)), array(‘attribute’ => ‘name’, ‘like’ => ‘M%’)));

    This gets all items whose weight is 1 or 3 OR whose name starts with M.