TL;DR: you need a unique index on the following combination of columns: entity_id
,attribute_id
,store_id
.
Long answer:
On an EAV entity (let's take products for example) your table could never look like that.
You have 2 records for the same combination of attribute_id | store_id | entity_id
.
On the product tables (catalog_product_entity_varchar
for example but it's the same for _int
and others) there is a unique constraint on these 3 columns.
UNIQUE KEY `UNQ_CAT_PRD_ENTT_VCHR_ENTT_ID_ATTR_ID_STORE_ID` (`entity_id`,`attribute_id`,`store_id`),
This is created in the install script (see install-1.6.0.0.php
)
->addIndex(
$installer->getIdxName(
array('catalog/product', 'varchar'),
array('entity_id', 'attribute_id', 'store_id'),
Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE
),
array('entity_id', 'attribute_id', 'store_id'),
array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE))
And when saving an attribute value (insert or update) a call to the method insertOnDuplicate
is made.
This method tries to insert the values you provide to it and if there is a unique key duplicate it just updates the values you specify. In the case of the EAV tables only the value
field is updated.
Since you don't have that unique constraint on the table it just inserts an other row.
Try adding that constraint and I have a feeling the problem will go away.
Best Answer
My examples are geared towards category image attribute, but the principle is the same for product based.
This information should get you going in the right direction (I hope)
Attributes have two elements that define their input and backend (processing) behaviour.
Below is the base requirements for a new attribute created in a category, which is an image. (the snippet is taken from a co-workers megamenu module (with some help from me in some areas, of course ;), and I include the ref to the code to allow you to view the snippet in context, which may clarify the code - [https://github.com/benjy14/MegaMenu/blob/master/app/code/local/DevBlog/MegaMenu/Model/Mysql4/Setup.php)
The important bits (for you) will be the
input
andbackend
elements.'input' => 'image'
relates to :Varien_Data_Form_Element_Image
(all the input element renderers live inlib/varien/data/form/element
'backend' =>
'catalog/category_attribute_backend_image', => relates to (well as it says)Mage_Catalog_Model_Category_Attribute_Backend_Image
:)in
Mage_Catalog_Model_Category_Attribute_Backend_Image
you will see a method 'afterSave' where processing takes place after the image is uploaded.for a custom input renderer, you will rather use 'input_renderer' (ref: http://inchoo.net/ecommerce/magento/adding-magento-attribute-with-custom-input-renderer/ I am not sure if 'input' will work for a custom renderer, but worth a try. (magento do change at times)
Thus, you just need to point the attribute to your custom input and backend models.
Hope this helps.