In the previous tutorial, we learn how to create the table using the Setup script and Declarative Schema. Models, Resource Models, and Collections in magento 2 are used for data operations. Models are an inherent part of the Model-View-Controller architecture. Models are divided into three parts models, resource models, and collections.
Now, first of all, we teach you the Model in Magento 2.
Models are the main business logic that should be handled. Every Model extends the Magento\Framework\Model\AbstractModel class, which inherits the \Magento\Framework\DataObject class. The Model will use the resource model to get or set the data of a model respectively.
Follow the below code for the Model and create a model file Contact.php in app/code/Dolphin/MyModule/Model.
<?php namespace Dolphin\MyModule\Model; use Magento\Framework\Model\AbstractModel; use Dolphin\MyModule\Model\ResourceModel\Contact as ContactResourceModel; class Contact extends AbstractModel { protected function _construct() { $this->_init(ContactResourceModel::class); } }
In the above code, you can see the Contact class has _construct() method with the use _init() method, and pass parameter resource model’s name.
Now, we are going to the resource model.
The resource model performs all actual database operations. Every CRUD resource model extends an abstract class Magento\Framework\Model\ResourceModel\Db\AbstractDb.
Create Contact.php file in app/code/Dolphin/MyModule/Model/ResourceModel.
<?php namespace Dolphin\MyModule\Model\ResourceModel; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; class Contact extends AbstractDb { protected function _construct() { $this->_init('dolphin_mymodule_contact', 'id'); } }
In the above code, you can see the Contact class use the method _construct() method with the use _init() method, and pass the two parameters. The first parameter uses the table name and the second parameter is the primary column in that table.
We can use the collections when we want to fetch data, join tables, select specific columns, apply WHERE clause into the query, or GROUP BY, ORDER BY. Collections are groups of models.
Create Collection.php file in app/code/Dolphin/MyModule/Model/ResourceModel/Contact/Collection.
<?php namespace Dolphin\MyModule\Model\ResourceModel\Contact; use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; use Dolphin\MyModule\Model\Contact as ContactModel; use Dolphin\MyModule\Model\ResourceModel\Contact as ContactResourceModel; class Collection extends AbstractCollection { protected function _construct() { $this->_init(ContactModel::class, ContactResourceModel::class); } }
All Collections are inherited the Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection class and use the method _construct() with the _init() method and pass the two parameters. The first parameter uses the Model class and the second one is the ResourceModel class. Collections have a lot of attributes and filters.
We hope our guide is very effective for you. If any questions, please feel free to leave a comment below. In the next tutorial, you will help with how to Perform CRUD Operation with a custom module in Magento 2.