我在更新 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();
}
关于php - Magento 1.9.1 更新自定义模块中的记录,尝试插入新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31160326/