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

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

我在 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();

这试图插入新记录而不是更新现有记录。插入失败,因为主键字段 'uuid' 获得重复条目

我也试过:

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

它仍然尝试插入而不是更新。

我想更新记录而不是插入新记录。

捕获到的异常是:"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '9b1c1b19-1fd7-11e5-9f7f-f46d04ac20c7' for key 'PRIMARY', query was: INSERT INTO....."如果系统更新而不是插入新的,则不会存在。

这样试试,

$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(); 
    }