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
Add comment
Follow Us
Popular Posts
Blog Categories
- Design (4)
- Development (13)
- Magento Development (32)
- PHP (5)
- Security (0)
- E-Commerce (6)
- General Business (6)
- Llama Culture (3)
- Magento (54)
- Management (4)
- Wiz (3)
- Online Marketing (1)
- Productivity (2)
- Uncategorized (2)



Comments
Guys, Thanks a lot for
Guys,
Thanks a lot for posting this. I had to do a mass attribute update for a huge product and was previously using the save method. The script took forever to run and hogged resources. I think it was because of the reindexing. This is a simple way to only update one attribute per product and its fast!