Magento 1.8 – Why Does ORM `hasDataChanges()` Behave Incorrectly?

magento-1.8model

I'm confused by the way Magento's ORM behaves when editing/saving an object; see the example below:

$model = Mage::getModel('vendor/model')
    ->setData(array(
        'name'        => 'Test,
        'description' => 'Info',
    ))
    ->save();

I now have a new record in the table, however this is where i'm confused.

$model = Mage::getModel('vendor/model')->load(1);
$model->setName('Test');  // THIS IS THE SAME
$model->setDescription('Info'); // THIS IS THE SAME

var_dump($model->hasDataChanges()); // returns true! (i expect false)
var_dump(($model->getData() !== $model->getOrigData())) // returns false (expected)

Surely the latter is how Magento should behave? Unless i'm missing something.

Best Answer

Looking into the setData function it appears that has data changes is always set to true even if technically the data has not changes.

This can be seen in Varien_Object function setData

public function setData($key, $value=null)
{
    $this->_hasDataChanges = true;
    if(is_array($key)) {
        $this->_data = $key;
        $this->_addFullNames();
    } else {
        $this->_data[$key] = $value;
        if (isset($this->_syncFieldsMap[$key])) {
            $fullFieldName = $this->_syncFieldsMap[$key];
            $this->_data[$fullFieldName] = $value;
        }
    }
    return $this;
}
Related Topic