简体   繁体   中英

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.

$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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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