2

There's a time issue I've noticed with saving new customers and the created_at attribute, where if you save the model twice, the time stored will be incorrect. To get around the issue you can call load() directly after the first save.

For example, you set the customer data, and then call save(). Created_at gets set with Mage_Eav_Model_Entity_Attribute_Backend_Time_Created::beforeSave() which uses Varien_Date::now(), which generates a date time stamp in UTC.

public function beforeSave($object)
{
    $attributeCode = $this->getAttribute()->getAttributeCode();
    $date = $object->getData($attributeCode);
    if (is_null($date)) {
        if ($object->isObjectNew()) {
            $object->setData($attributeCode, Varien_Date::now());
        }
    } else {
        // convert to UTC
        $zendDate = Mage::app()->getLocale()->utcDate(null, $date, true, $this->_getFormat($date));
        $object->setData($attributeCode, $zendDate->getIso());
    }

    return $this;
}

Customer_entity.created_at is mysql type timestamp, not datetime, so when this gets saved, it also stores the timezone. After the first save, the time in memory is in UTC, and the column is in UTC (+0000).

After the second save, beforeSave() gets called again. However, the object is not considered new anymore and it assumes the time in memory is stored in the store local time, as that's what afterLoad() does. The time gets incorrectly converted from store local time to UTC time before saving, when the time in memory is actually still in UTC, so the time stored has actually twice the intended offset.

So I'm curious, is this a Magento bug? Or is it considered standard practice that after the first time you save a model, that that is considered the end of life for that object? That would not make sense, because of course people may want to create after_save observers for that object and perform additional tasks with it. Would love to hear anyone's input.

1

Every model or at least the abstract model class that every model inherits from does have a _afterSave() function where it dispatches the after save observers from. So this does sound like a Magento bug.

/**
 * Processing object after save data
 *
 * @return Mage_Core_Model_Abstract
 */
protected function _afterSave()
{
    $this->cleanModelCache();
    Mage::dispatchEvent('model_save_after', array('object'=>$this));
    Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
    return $this;
}
  • I also spoke with Alan Storm about this, he seems to believe it is a magento bug as well. I'm probably going to take the next step and report it on their official bug tracker. – Paul Wisniewski Sep 8 '14 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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