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]:完整性约束冲突:1062键'PRIMARY'的重复条目'9b1c1b19-1fd7-11e5-9f7f-f46d04ac20c7',查询为: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(); 
    }