3

I create a custom attribute last_sync for customer custom attribute, i need to filter it if this attribute is null OR greater than created_at, right now i'm using code like this:

$collections = Mage::getModel('customer/customer')->getCollection()
        ->addAttributeToSelect('*');

foreach ($collections as $collection)
{
  $result = array();
  $result = $collection->getData();
  if(is_null($result['last_sync']) || ($result['last_sync'] < $result['updated_at']))
  {  
     $results[] = $result;
  }
}
return $results;

the problem for the above code is the speed time, because i have to loop through each customer data than filter it

1 Answer 1

1

You can try

$collections = Mage::getModel('customer/customer')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter('last_sync', array(
        array('null' => true),
        array('lt' => new Zend_Db_Expr('updated_at'))
    ));

or

$collections = Mage::getModel('customer/customer')->getCollection()
    ->addAttributeToSelect('*');

$collections->getSelect()->where(
    "(last_sync IS NULL) OR (last_sync < updated_at)"
);

It will also be faster if you only selected the attributes that you actually want to use.

2
  • how to combine it with is_null() in OR operator like my if condition in question above? Oct 24, 2017 at 10:16
  • I updated the answer. Haven't tested this though, so it may need tweaking Oct 24, 2017 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.