Blog

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

Comments

Thomas's picture

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!

Add comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options