Tag Archives: duplicate attribute row

Magento: Rows are not updated in custom EAV model tables

Another day, another annoying Magento issue. I’ve created a new EAV model, and when I thought that all things are finished, I’ve found out that in the model’s entity tables (_int, _varchar, etc.) the rows are not updated, but always inserted.
Viktor Hesselbom’s article pointed out that the problem’s root is in _updateAttribute method.
What remained to me is to fix it. You will have to override in your resource model the _updateAttribute method. Here is the solution:

 

protected function _updateAttribute($object, $attribute, $valueId, $value)
{
    $table = $attribute->getBackend()->getTable();
    if (!isset($this->_attributeValuesToSave[$table])) {
        $this->_attributeValuesToSave[$table] = array();
    }

    $entityIdField = $attribute->getBackend()->getEntityIdField();

    $data   = array(
        'entity_type_id'    => $object->getEntityTypeId(),
        $entityIdField      => $object->getId(),
        'attribute_id'      => $attribute->getId(),
        'value'             => $this->_prepareValueForSave($value, $attribute)
    );
    if ($valueId)
    {
        $data['value_id'] = $valueId;
    }

    $this->_attributeValuesToSave[$table][] = $data;

    return $this;
}

Only the value_id has been added to the $data array, if $valueId is set. In rest it is the same as the original method.
Good luck!