Alternative to Magento 2 Deprecated Load, Save and Delete Methods
Developers, you might be aware that Magento 2 deprecated the Abstract model’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. π
In the default AbstractModel of Magento 2, load(), save(), and delete() are the essential methods to perform the basic CRUD operations in the database. 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.
You may also receive the PHP function deprecated error from the code editor/IDE.
In this blog post, I will provide you the best alternative to Magento 2 deprecated load, save and delete methods.
Magento 2 Deprecated Load, Save & Delete Methods – Alternative Solution
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 resourceModel
.
Provided below is the example of using the resourceModel
for the load, save, and delete methods as an alternative to the AbstractModel in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
use Exception; use Vendor\Extension\Model\AgencyPortalFactory as ExtensionModelFactory; use Vendor\Extension\Model\ResourceModel\AgencyPortalFactory as ExtensionResourceModelFactory; class YourClass { private $extensionModelFactory; private $extensionResourceModelFactory; public function __construct( ExtensionModelFactory $extensionModelFactory, ExtensionResourceModelFactory $extensionResourceModelFactory ) { $this->extensionModelFactory = $extensionModelFactory; $this->extensionResourceModelFactory = $extensionResourceModelFactory; } public function execute() { try { $id = '1'; $model = $this->extensionModelFactory->create(); $resourceModel = $this->extensionResourceModelFactory->create(); $resourceModel->load($model, $id); // for load $resourceModel->save($model); // for save $resourceModel->delete($model); // for delete } catch (Exception $e) { \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class)->info($e->getMessage()); } } } |
You can use the load(), save(), and delete() methods using the above snippet of code in Magento 2 without any hassle.
That’s it!
Still have any doubts? Let me know through the comments; I’d be happy to help you.
I hope this blog post will help you enrich your Magento 2 coding knowledge. π¨βπ»
Happy with this Magento solution? Rate this blog post with 5 stars to let me know. π€©
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.
Thanks for reading! π
Siddharth Nandava
Siddharth Nandava is an enthusiastic Jr Magento developer at Meetanshi. Apart from the work, you can find him learning new things and spending quality time with his family.
Prev
10 Coupon Statistics You Need to Know [2024]
How to Charge Sales Tax on Shopify
Next