![](/img/trans.png)
[英]PHP/MySQL - UPDATE Query inserts new record instead updating the record
[英]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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.