{"id":2199,"date":"2022-11-22T07:30:07","date_gmt":"2022-11-22T07:30:07","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/alternative-to-magento-2-deprecated-load-save-and-delete-methods\/"},"modified":"2025-03-17T07:23:15","modified_gmt":"2025-03-17T07:23:15","slug":"alternative-to-magento-2-deprecated-load-save-and-delete-methods","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/alternative-to-magento-2-deprecated-load-save-and-delete-methods\/","title":{"rendered":"Alternative to Magento 2 Deprecated Load, Save and Delete Methods"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Developers, you might be aware that Magento 2 deprecated the Abstract model&#8217;s load, save, and delete methods. Using the same in your code can throw errors and cause compatibility issues in the future. Read this blog post to find an alternative to the Magento 2 deprecated load, save, and delete methods.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the default AbstractModel of Magento 2, load(), save(), and delete() are the essential methods to <a title=\"How to Create File to Perform CRUD Operation in Magento 2\" href=\"https:\/\/meetanshi.com\/blog\/create-model-file-to-perform-crud-operation-in-magento-2\/\" target=\"_blank\" rel=\"noopener\">perform the basic CRUD operations in the database<\/a>. Although there are innumerable classes in the core that extend the AbstractModel class and use these methods, it is advised to avoid using these deprecated methods, as they can cause a compatibility issues in the future.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may also receive the PHP function deprecated error from the code editor\/IDE.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this blog post, I will provide you the best <em><strong>alternative to Magento 2 deprecated load, save and delete methods<\/strong><\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Magento 2 Deprecated Load, Save &amp; Delete Methods &#8211; Alternative Solution<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Many Magento 2 developers are confused because of the deprecation of the load(), save(), and delete() methods of the AbstractModel class. The best alternative to these deprecated methods is to use the <code>resourceModel<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Provided below is the example of using the <code>resourceModel<\/code> for the load, save, and delete methods as an alternative to the AbstractModel in Magento 2:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Exception;\nuse Vendor\\Extension\\Model\\AgencyPortalFactory as ExtensionModelFactory;\nuse Vendor\\Extension\\Model\\ResourceModel\\AgencyPortalFactory as ExtensionResourceModelFactory;\n\nclass YourClass\n{\n    private $extensionModelFactory;\n    private $extensionResourceModelFactory;\n\n    public function __construct(\n        ExtensionModelFactory $extensionModelFactory,\n        ExtensionResourceModelFactory $extensionResourceModelFactory\n    ) {\n        $this->extensionModelFactory = $extensionModelFactory;\n        $this->extensionResourceModelFactory = $extensionResourceModelFactory;\n    }\n\n    public function execute()\n    {\n        try {\n            $id = '1';\n            $model = $this->extensionModelFactory->create();\n            $resourceModel = $this->extensionResourceModelFactory->create();\n            $resourceModel->load($model, $id); \/\/ for load\n            $resourceModel->save($model); \/\/ for save\n            $resourceModel->delete($model); \/\/ for delete\n        } catch (Exception $e) {\n            \\Magento\\Framework\\App\\ObjectManager::getInstance()->get(\\Psr\\Log\\LoggerInterface::class)->info($e->getMessage());\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the load(), save(), and delete() methods using the above snippet of code in Magento 2 without any hassle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Still have any doubts? Let me know through the comments; I&#8217;d be happy to help you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope this blog post will help you enrich your Magento 2 coding knowledge.  \u200d <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy with this Magento solution? Rate this blog post with 5 stars to let me know.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, sharing it without your Magento friends via social media is totally free! It will encourage us to post more solutions to real-life problems.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading!  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Developers, you might be aware that Magento 2 deprecated the Abstract model&#8217;s load, save, and delete methods. Using the same in your code can throw&#8230;<\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2199","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2199"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2199\/revisions"}],"predecessor-version":[{"id":9469,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2199\/revisions\/9469"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}