简体   繁体   English

Magento 1.9.1更新自定义模块中的记录,尝试插入一个新记录

[英]Magento 1.9.1 Updating a record in a custom module, tries to inserts a new one instead

I'm facing an issue with updating a record on MAGENTO 1.9.1 community edition. 我在更新MAGENTO 1.9.1社区版的记录时遇到问题。

$model = Mage::getModel("module/tablemodel")->load($uuid,'uuid'); //uuid is the PK// No id field in the table
$code = $model->getCode();

echo $code;
/*Works fine as it prints the code in the table for the corresponding row, hence I'm sure the model is loaded fine*/


$data = array('status'=>1,'modified_datetime'=>date('Y-m-d H:i:s'));
$model->addData($data);
$modelApprovalLog->save();

This is trying to insert a new record rather than updating the existing record. 这是在尝试插入新记录,而不是更新现有记录。 Inserting fails as the primary key field 'uuid' gets a duplicate entry 由于主键字段“ uuid”获得重复的条目,因此插入失败

I've also tried: 我也尝试过:

$model->setStatus(1);
$model->setModifiedDatetime(date('Y-m-d H:i:s'));
$model->save();

It still tries to insert rather than update. 它仍然尝试插入而不是更新。

I want to update the record and not insert a new one. 我想更新记录,而不要插入新记录。

Exception caught is: "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '9b1c1b19-1fd7-11e5-9f7f-f46d04ac20c7' for key 'PRIMARY', query was: INSERT INTO....." which won't be there if the system updates rather than inserting a new one. 捕获到的异常是:“ SQLSTATE [23000]:违反完整性约束:1062键'PRIMARY'的条目'9b1c1b19-1fd7-11e5-9f7f-f46d04ac20c7'复制了,查询为:INSERT INTO .....”如果系统更新而不是插入新的,则在那里。

Try like this, 这样尝试

$data  = array('status'=>1,'modified_datetime'=>date('Y-m-d H:i:s'));
$model = Mage::getModel("module/tablemodel")->load($uuid)->addData($data);

    try {
            $model->setId($uuid)->save();
            echo "Data updated successfully.";

        } catch (Exception $e){
            echo $e->getMessage(); 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM