質問

I have a custom Magento module with EAV structure. It can create, edit, delete and listing items.

When I edit and save an item the attribute value doesn't replace by new value in the database. On the admin, I see the new value but in the database the old value exist too.

What I see as admin user:
- Item editing
- Changing Name from Test 1 t Test 2
- Save is successful
- Now, the item's name is Test 2

What I see in the database:
- value_id, entity_type_id, attribute_id, store_id, entity_id, value
- Old row: 5, 31, 961, 0, 5, Test 1
- New row: 6, 31, 961, 0, 5, Test 2

In the code:

public function saveAction()
{
    if ($postData = $this->getRequest()->getPost()) {
        $model = Mage::getSingleton('mynamespace/model');

        $model->setData($postData);

        if ($this->getRequest()->getParam('id')) {
            $model->setId($this->getRequest()->getParam('id'));
        }

        try {
            $model->save();

            Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Item has been saved.'));
            $this->_redirect('*/*/');

            return;
        }
        catch (Mage_Core_Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($this->__('An error occurred while saving this item.'));
        }

        $this->_redirectReferer();
    }
}

First time I have one row. After save I have two rows, after save again I have three... Why setData() or setName() functions can not overwrite/update the old rows? Why does it create new rows? How can I fix it?

Installer file:

$installer = $this;
$installer->startSetup();

$eavTableName = 'loremipsum/lorem';

$installer->addEntityType(
    'loremipsum_lorem', array(
        'entity_model'    => $eavTableName,
        'table'           => $eavTableName
    )
);

$installer->createEntityTables(
    $this->getTable('loremipsum/lorem')
)->addIndex(
    $this->getIdxName(
        $eavTableName,
        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)
);

$this->addAttribute('loremipsum_lorem', 'name', array(
    'type'              => 'varchar',
    'label'             => 'Name',
    'input'             => 'text',
    'class'             => '',
    'backend'           => '',
    'frontend'          => '',
    'source'            => '',
    'required'          => true,
    'user_defined'      => true,
    'default'           => '',
    'unique'            => false
));

$installer->endSetup();
役に立ちましたか?

解決

Try overriding in your resource model the _updateAttribute method

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 is added to the $data array if one found. That solves the problem. This solution can be also found at: http://code007.wordpress.com/2014/03/24/magento-rows-are-not-updated-in-custom-eav-model-tables/

他のヒント

It looks like the problem is related to a missing unique index -_-

try adding a unique index to your entity tables(_int, _varchar, _decimal, ....) over following cols

entity_id, attribute_id, store_id

after i added these one it worked for me

if you want to fix your install script: you will need to create/update you Setup Class

the problem is in the Mage_Eav_Model_Entity_Setup::createEntityTables function which is missing the unique indexes. copy that function into your setup class and add

            ->addIndex(
                $this->getIdxName(
                    $eavTableName,
                    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));

into the creation statement of the eav_tables.
after that the foreach loop that creates the eav tables should look like this:

    /**
     * Create table array($baseTableName, $type)
     */
    foreach ($types as $type => $fieldType) {
        $eavTableName = array($baseTableName, $type);

        $eavTable = $connection->newTable($this->getTable($eavTableName));
        $eavTable
            ->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
                'identity'  => true,
                'nullable'  => false,
                'primary'   => true,
                'unsigned'  => true,
            ), 'Value Id')
            ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
                'unsigned'  => true,
                'nullable'  => false,
                'default'   => '0',
            ), 'Entity Type Id')
            ->addColumn('attribute_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
                'unsigned'  => true,
                'nullable'  => false,
                'default'   => '0',
            ), 'Attribute Id')
            ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
                'unsigned'  => true,
                'nullable'  => false,
                'default'   => '0',
            ), 'Store Id')
            ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
                'unsigned'  => true,
                'nullable'  => false,
                'default'   => '0',
            ), 'Entity Id')
            ->addColumn('value', $fieldType[0], $fieldType[1], array(
                'nullable'  => false,
            ), 'Attribute Value')
            ->addIndex($this->getIdxName($eavTableName, array('entity_type_id')),
                array('entity_type_id'))
            ->addIndex($this->getIdxName($eavTableName, array('attribute_id')),
                array('attribute_id'))
            ->addIndex($this->getIdxName($eavTableName, array('store_id')),
                array('store_id'))
            ->addIndex($this->getIdxName($eavTableName, array('entity_id')),
                array('entity_id'))
            ->addIndex(
                $this->getIdxName(
                    $eavTableName,
                    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));
        if ($type !== 'text') {
            $eavTable->addIndex($this->getIdxName($eavTableName, array('attribute_id', 'value')),
                array('attribute_id', 'value'));
            $eavTable->addIndex($this->getIdxName($eavTableName, array('entity_type_id', 'value')),
                array('entity_type_id', 'value'));
        }

        $eavTable
            ->addForeignKey($this->getFkName($eavTableName, 'entity_id', $baseTableName, 'entity_id'),
                'entity_id', $this->getTable($baseTableName), 'entity_id',
                Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
            ->addForeignKey($this->getFkName($eavTableName, 'entity_type_id', 'eav/entity_type', 'entity_type_id'),
                'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id',
                Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
            ->addForeignKey($this->getFkName($eavTableName, 'store_id', 'core/store', 'store_id'),
                'store_id', $this->getTable('core/store'), 'store_id',
                Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
            ->setComment('Eav Entity Value Table');

        $tables[$this->getTable($eavTableName)] = $eavTable;
    }

If you have already created the entity and you dont want to reinstall/drop the tables, try something like this untested

//your entity types
$entityTypes = array('datetime', 'decimal', 'int', 'text', 'varchar');
foreach($entityTypes AS $type){
    $connection->addIndex(
        $this->getTable('loremipsum/lorem') . '_' . $type,
        $installer->getIdxName(
            $eavTableName,
            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)
    );
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top