Magento, Magento 2

Model, Resource Model and Collections

Model, Resource Model and Collections

Hello Everyone, Welcome to my blog. First of all for those people who don’t know anything about me please check out this link for more information. 

If you want to connect with me or want to share your view or anything, please feel free to write a comment below or write me an email on my email id i.e ranade.ashish11@gmail.com. I will definitely try to address all your queries.

Let’s Get Started

MVC is a one of the design patterns where we organize our code in a structured way. From the MVC, M stands for the model which is an inherent part of the MVC pattern. The model is used to create a bridge between the controller, view and database. We can write our application business logic models. Magento models are divided into three parts: Model, Resource model and Collection Model.

Model

The model contains the application’s business logic and depends on an associated class – the resource model – for database access. All the data fetching and manipulation is performed in models. Every database table in Magento is associated with a model. Models and written in the Model directory on your module, i.e. Namespace/ModuleName/Model. In our case it would be in Ashish\FirstModule\Model.

Model Directory
Model, Resource Model and Collections 3

Every model class inherits from Magento\Framework\Model\AbstractModel class, which further extends from Magento\Framework\DataObject. DataObject class allows us to use some important functions like setData () and GetData () etc., which will help us to set the data on a model and save it in the database table.

To Demonstrate, I have created one model FirstModule in Ashish\FirstModule\Model

<?php

namespace Ashish\FirstModule\Model;

use Magento\Framework\Model\AbstractModel;
use Ashish\FirstModule\Model\ResourceModel\FirstModule as ResourceModel;

class FirstModule
    extends AbstractModel
{
    /**
    * construct
    */
    protected function _construct()
    {
        $this->_init(ResourceModel::class);
    }
}

The FirstModule class has a method _construct() in which I have called the _init() method and passed my Resource model class as parameter. _init() method is used to set the resource model class and primary key field. 

protected function _init($resourceModel)
{
    $this->_setResourceModel($resourceModel);
    $this->_idFieldName = $this->_getResource()->getIdFieldName();
}

Resource Model

Resource model is used to perform actual CRUD operation on database tables, every model must have a resource model, since all of the methods of a resource model expects a model as its first parameter. The resource model extends from Magento\Framework\Model\ResourceModel\Db\AbstractDbclass . The resource model resides under the Namespace\ModuleName\Model\ResourceModel directory. In our case it would be in Ashish\FirstModule\Model\ResourceModel\FirstModule.

<?php

namespace Ashish\FirstModule\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class FirstModule
      extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('my_first_module', 'module_id');
    }
}

Our resource module consists of _construct() method in which we have called _init() method. _init() method accepts two parameters one is the main table, table which is associated with our module in this case “my_first_module” and the second one is the primary key of that table i.e. "module_id"

protected function _init($mainTable, $idFieldName)
{
    $this->_setMainTable($mainTable, $idFieldName);
}

Collections

The collections are used to fetch the multiple rows from the database. Collection can be use when we want to 

  1. Fetch multiple rows from a table
  2. Join tables with our primary table
  3. Select specific columns
  4. Apply a WHERE clause to our query
  5. Use GROUP BY or ORDER BY in our query

Let’s create our collection class in Ashish\FirstModule\Model\ResourceModel\FirstModule\Collection.php 

<?php
namespace Ashish\FirstModule\Model\ResourceModel\FirstModule;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Ashish\FirstModule\Model\FirstModule as MyModel;
use Ashish\FirstModule\Model\ResourceModel\FirstModule as MyResourceModel;

class Collection
  extends AbstractCollection
{
   /**
   * Constructor
   */
    protected function _construct()
    {
        $this->_init(MyModel::class, MyResourceModel::class);
    }
}

Collection class must be extended from Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection. We have called _init() method from our _construct() methods which accept two parameters i.e. model class and another one is resource model class.

That’s all for now. In the next post I will explain how to save and fetch the data using models. Feel free to leave a comment below. Let me know what you think about this blog. Good bye and happy coding 🙂

[post-views]

Tagged ,

Leave a Reply

Your email address will not be published. Required fields are marked *