我有一个自定义的Magento模块与EAV结构。它可以创建,编辑,删除和列出项目。Magento自定义模块与数据库时EAV重复属性行saveAction()
当我编辑并保存一个项目时,属性值不会被数据库中的新值所取代。在管理员上,我看到了新的价值,但在数据库中,旧价值也存在。
我所看到的管理员用户:
- 项目编辑
- 从测试1 t检验2
更改名称 - 保存成功
- 现在,该项目的名称是Test 2
我看到在数据库内容:
- value_id,entity_type_id,attribute_id,STORE_ID,ENTITY_ID,价值
- 旧行:5,31,961,0,5,测试1
- 新行:6,31,961,0,5,试验2
在代码:
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();
}
}
我第一次一行。保存后我有两行,保存后我有三个... 为什么setData()或setName()函数不能覆盖/更新旧行?为什么它会创建新的行?我该如何解决它?
安装程序文件:
$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();
谢谢,但唯一的索引对我的情况并没有影响到这个问题。我遵循你的指示,但问题是一样的。我根据你的回答扩展了我的问题。 – David
ahh ok找到了问题。你将索引应用到你的主实体表中,这是错误的,你需要将它应用到实体表_int,_varchar等,我想我明确表示:)。我将以这种方式修改我的答案 –
我厌倦了它,但是这个代码 - 在最后一个块中 - 不会添加唯一索引。我通过安装脚本函数“run”成功添加了唯一索引,但是当entity_id,attribute_id和store_id是唯一索引时,新行将覆盖最后一行。现在,当数据库中有更多项目时,只有一个项目具有属性,因为attribute_id是唯一的。在这种情况下,另一个项目不能具有相同的attribute_id,例如title属性。 – David