Magento 2 Load, Save and Delete Deprecated Methods Alternatives

As Magento is going to remove the Model abstract’s methods(save, load and delete).
we have 2 options OR alternatives.
1. Use Repository
2. Use resource Model

Repository interface implementation in Model Example
/**
* Save test data.
/ public function save(\Vendor\Module\Api\Data\TestInterface $test) { //your code } /*
* Retrieve test data.
/ public function getById($testId) { //your code } /*
* Delete test.
/ public function delete(\Vendor\Module\Api\Data\TestInterface $test) { //your code } /*
* Delete test by test ID.
*/
public function deleteById($testId)
{
//your code
}

Resource Model Example :
try {
$resourceModel = $this->resourceModelFactory->create();
$resourceModel->load($model, $id); // for load
$resourceModel->save($model); // for save
$resourceModel->delete($model); // for delete
} catch (Exception $e) {
ObjectManager::getInstance()->get(LoggerInterface::class)->info($e->getMessage());
}


please give it a try and let me know if you want me to add it in details 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top