title: Programmatically updating/editing an attribute's option in Magento
body: <p>I have an attribute called "vendor_id". I have N products that have the "vendor_id" as an attribute of the product. The options for the "vendor_id" attribute are being generated programmatically when an admin adds a new "vendor" entity. The code is like this:</p>
  
  <pre><code>    public function saveAction()
      {
      $data = $this-&gt;getRequest()-&gt;getPost();
      $designerName = $data['title'];
  
      $product = Mage::getModel('catalog/product');
      $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
              -&gt;setEntityTypeFilter($product-&gt;getResource()-&gt;getTypeId())
              -&gt;addFieldToFilter('attribute_code', 'vendor_id')
              -&gt;load(false);
      $attribute = $attributes-&gt;getFirstItem()-&gt;setEntity($product-&gt;getResource());
      $myresults = array ('value'=&gt; array('optionone'=&gt;array($designerName))); 
  
      $attribute-&gt;setData('option',$myresults);
      $attribute-&gt;save(); 
  </code></pre>
  
  <p>And this works for now. It will create an option for the "vendor_id" attribute, and when the user adds a new product (or edit's an existing product) the drop down for "vendor_id" will be populated by these "vendor" entities we've created on the saveAction() method.</p>
  
  <p>Now, in the case an admin wants to edit an existing attribute option, I don't want to create a new option, I want to edit an existing one. It's important that the option ID stays the same when we change the name/label.</p>
  
  <p>I've hooked in the newAction to set a static var so in the saveAction() we can check and see if we are editing or creating a new option:</p>
  
  <pre><code>    if (null == MyController::$_editScope)
      {
          error_log('Need to update option attribute');
          $attribute-&gt;addData($myresults);
      }
  </code></pre>
  
  <p>The problem is that the addData() method does just that, it adds the data, but doesn't update the existing on. The attribute is: </p>
  
  <pre><code>    $attribute = $attributes-&gt;getFirstItem()-&gt;setEntity($product-&gt;getResource());
  </code></pre>
  
  <p>Which is an instance of: <a href="http://docs.magentocommerce.com/Mage_Eav/Mage_Eav_Model_Entity_Attribute.html" rel="nofollow">http://docs.magentocommerce.com/Mage_Eav/Mage_Eav_Model_Entity_Attribute.html</a></p>
  
  <p>Which has 3x parent classes and I've looked through them all for a method that will allow me to edit* or update* an existing option's name ...</p>