I ran into weird issue. I am trying to update my custom model/table in catalog_product_save_after event but it always creates new entry in the DB instead of updating older one. I'm doing this way :

        $model = $this->_qpcFactory->create();
        $id = $this->getQPCId();
//if I print model here it is working fine, i-e same model loaded that I want.
//but here new record is created in my custom table instead of updating the one I loaded above. 

Is it something to do with the event I am using because this works smooth in Save controller? Any clue will be appreciated.

1 Answer 1


Consider using catalog_product_save_commit_after as the transaction from saving the catalog_product has not been commit on catalog_product_save_after.

Also create a repository or use the resource model of your QPC model to load and save your model.

// I assume here you injected the QPC resource model in the constructor
// as "qpcResource".
$qpcModel = $this->_qpcFactory->create();
$id = $this->getQPCId();
if($id) {            
    $this->qpcResource->load($qpcModel, $id);
    $qpcModel->setData($data); // Maybe $data has not the id?

Using setData with an array completely overrides the data of your model. If you do not need to override the entire data of your model, consider only setting the necessary data instead of overriding everything. Check if $data contains the id of the original data of the $qpcModel. If not, add it to $data. Magento will create a new entry in the db when the _isObjectNewFlag on the model is set, or when the id is missing.

  • I knew it would be a silly mistake. The id wasn't set in $data. :) Aug 23, 2021 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.