How to Create Model, Resource Model, and Collection in Magento 2

In this article, We're going to explain you how to Create Model, Resource Model, and Collection in Magento 2. After following few simple steps of this article you are able to easily create Model, Resource Model, and Collection.

In our previous articles we've learn:

I hope you have understood and applied those steps easily. Now, in this article, we will go more deeper and see how to create Model, Resource Model and Collection in Magento 2.

After complate this step, you'll able to perform CRUD operation (insert, update, delete and get data from the custom table).

Let's get started!!

Follow the simple steps which are listed below.

Step 1: Create Model HelloWorld.php at app/code/Devhooks/HelloWorld/Model/ and paste the below code.

saveCopyzoom_out_map
<?php namespace Devhooks\HelloWorld\Model; use Devhooks\HelloWorld\Model\ResourceModel\HelloWorld as HelloWorldResourceModel; use Magento\Framework\Model\AbstractModel; class HelloWorld extends AbstractModel { protected function _construct() { $this->_init(HelloWorldResourceModel::class); } }

Step 2: Create Resource Model HelloWorld.php file at app/code/Devhooks/HelloWorld/Model/ResourceModel/ and paste the below code.

saveCopyzoom_out_map
<?php namespace Devhooks\HelloWorld\Model\ResourceModel; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; class HelloWorld extends AbstractDb { protected function _construct() { $this->_init('devhooks_helloworld', 'id'); } }

Step 3: Create Collection Collection.php file at app/code/Devhooks/HelloWorld/Model/ResourceModel/HelloWorld/ and paste the below code.

saveCopyzoom_out_map
<?php namespace Devhooks\HelloWorld\Model\ResourceModel\HelloWorld; use Devhooks\HelloWorld\Model\HelloWorld as HelloWorldModel; use Devhooks\HelloWorld\Model\ResourceModel\HelloWorld as HelloWorldResourceModel; use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; class Collection extends AbstractCollection { protected function _construct() { $this->_init( HelloWorldModel::class, HelloWorldResourceModel::class ); } }

Now, open Command line in folder root of magento and run both commands

saveCopyzoom_out_map
php bin/magento setup:upgrade php bin/magento cache:flush

We have successfully complated our third step, we hope this article helped you to create Model, Resource Model, and Collection in Magento 2. Now our basic module is created and we are ready for display our records on UI grid. But before that we need to create an admin menu for navigate to the new grid. So, let's move to the next step and see How to Add Custom Admin Menu In Magento 2.

Stay Tuned...

Bookmark it for your future reference. Do comment below if you have any other questions or doubts.

P.S. Do share this post with your team.

Special thanks to Rutvik Monpara, He helped me a lot to write a series of How to Create UI Component Grid and Form in Magento 2

Review other articles maybe it'll help you too.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off