Saving the Value of a Specific Attribute from a Model
In Magento, it's very easy to save all the data in a model by running $model->save();. (Note: In this blog post, model refers to an EAV model, not a flat resource model) This saves all the attributes for the model to their respective attribute tables. There are times when saving the value of just one of the model attributes is desirable. A couple cases where you'd want to do this:
- You've been passed a model from an event, and you aren't sure if the data in that model can be safely saved.
- You are saving many models and want to make your save operations as efficient as possible
Here is how you'd save just one attribute of a product model:
$product = Mage::getModel('catalog/product')->load(1); $product->setName('Some Random Name'); $product->getResource()->saveAttribute($product, 'name');
Posted on February 10, 2010
Posted by Erik Hansen