Steven Zurek
Steven Zurek

Reputation: 525

Updating Attribute in Magento Error

I'm consuming json requests from an outside server to update products and such in Magento (please don't ask why we didn't just use the the api's ... ) and I'm now trying to update the attribute information and it seems to be failing the _uniqueCheck() method in the resource abstract. The problem is i don't know how to fix it. The following code

$attribute_model = Mage::getModel('eav/entity_attribute');
$attributeId = $attribute_model->getIdByCode('catalog_product', $attribute_code);
$attribute = $attribute_model->load($attributeId);

$attribute->setData($data)->save();

results in the following error:

[29-May-2013 16:32:00 UTC] PHP Fatal error:  Uncaught exception 'Mage_Core_Exception' with message 'Attribute with the same code already exists.' in .../app/Mage.php:594
Stack trace:
#0 .../app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(676): Mage::throwException('Attribute with ...')
#1 .../app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(424): Mage_Core_Model_Resource_Db_Abstract->_checkUnique(Object(Mage_Eav_Model_Entity_Attribute))
#2 .../app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Core_Model_Resource_Db_Abstract->save(Object(Mage_Eav_Model_Entity_Attribute))
#3 .../app/code/local/Valoran/Import/Model/Attribute.php(25): Mage_Core_Model_Abstract->save()
#4 .../app/code/local/Valoran/Harmony/Model/Messaging/Attributeset.php(115): Valoran_Import_Model_Attribute->_create(Array)
#5 .../app/code/local/Valoran/Harmony/Model/Messaging/Attributeset.php(23): Valoran_Harmony_Model_Messaging_Attribut in .../app/Mage.php on line 594

This is frustrating for me because I know the code all ready exists, I'm trying to do an update that's why I called ->load($id) ....

Does anyone have any thoughts on what I'm missing here? I'm now exploring using the Mage_Eav_Model_Entity_Setup::updateAttribute method to accomplish this but so far I'm running into the same error.

Upvotes: 2

Views: 2270

Answers (1)

Zefiryn
Zefiryn

Reputation: 2265

I see two possible reasons why this is happening. Both of them are related with the use of setData() method. When you call it with an array as argument it will just replace the protected property $_data of the model. This property contains all information that are loaded/saved to the database.

I suspect that in your $data variable you do not have entity_attribute_id key. If this is the case when calling setData() you are removing id of the loaded attribute. You might be using attribute_code that is already used by another attribute in the system. Anyway when you are saving the attribute magento checks for entity_attribute_id and if it is empty magento assumes that you want to create new attribute so it performs validation for the new attribute. This results in finding an attribute with the same code which is illegal and throws error. This attribute might be the one you loaded but you have removed the entity_attribute_id property or another with the same attribute_code

If you do not want to use Mage_Eav_Model_Entity_Setup::updateAttribute you could try calling

$attribute = $attribute_model->load($attributeId);
$data['entity_attribute_id'] = $attribute->getEntityAttributeId();
$attribute->setData($data)->save();

Upvotes: 1

Related Questions