What is the difference between $model->getOrigData(); and $model->getData();?

The question:

There are two methods which retrieve model data.


Can anyone explain what is the difference between them ? Any help will be appreciated.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

getOrigData() returns the data in the object at the time it was initialised/populated.

After the model is initialised you can update that data and getData() will return what you currently have in that object.

Have a look at Varien_Object (getOrigData,setOrigData), grep -R "origData" app/ so you can have a look at what is used.

In EE, it’s very useful to log what is changed (Enterprise_Logging) module.

Method 2

This is easiest explained by an example:

$product = Mage::getModel('catalog/product')->load(123);
$product->getData('something'); // returns "foo"
$product->setData('something', 'bar');
$product->getData('something');     // returns "bar"
$product->getOrigData('something'); // returns "foo"

In a nutshell, every model (once loaded) will make a copy of the originally loaded data and store it in Model_Class::_origData property. This makes it possible to do optimizations for _beforeSave calls so that queries only modify the changed data and not rewrite the same data all the time.

Also, any time you call setData() a flag is set that the model has changed data. You can check if a model has changed data by calling $model->hasDataChanges().

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment