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:
- How To Create Magento 2 Module
- How to Create Custom Database in Magento 2
- How to Insert Sample Data in Magento 2
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_mapphp 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.
AI-Powered Recommended Articles
How To Create Magento 2 Module with Admin Grid and Form using UI Component
Learn how to create a custom Magento 2 module with an admin grid and form using UI components.
How to Create UI Component Grid In Magento 2
Learn how to create a custom UI component grid in Magento 2 for enhanced administrative functionality.
How to Create UI Component Form and CRUD Operation In Magento 2
Learn how to create UI component forms and implement CRUD operations in Magento 2 for efficient backend management.
How to Create Custom Table and Insert Sample Data in Magento 2
Create custom tables in Magento 2 and insert sample data for testing or customization purposes.
How to Add Mass Action in UI Component Grid in Magento 2
Learn how to add mass actions to Magento 2 UI component grids to enhance administrative efficiency.
How to Insert Multiple Records using ResourceConnection in Magento 2
Learn how to insert multiple records efficiently in Magento 2 using resource connection to manage database operations effectively.