---
title: "Models, Resource Models and Collections in Magento 2"
url: "https://dolphinwebsolution.com/models-resource-models-and-collections-in-magento-2"
date: "2021-03-27T06:15:08+05:30"
modified: "2023-08-24T10:09:55+05:30"
author:
  name: "Payal Patel"
categories:
  - "Magento"
word_count: 484
reading_time: "3 min read"
summary: "Models, Resource Models, and Collections are used for data operations. Models are an inherent part of the Model-View-Controller architecture. Models are divided into three parts models, resource mo..."
description: "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..."
keywords: "Collections in Magento 2,resource models in magento 2,models in magento 2,magento 2,magento plugins, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to add additional options in magento 2 cart"
    url: "https://dolphinwebsolution.com/how-to-add-additional-options-in-cart"
  - title: "How to Enable WYSIWYG editor"
    url: "https://dolphinwebsolution.com/how-to-enable-wysiwyg-editor"
  - title: "How To Show Salable Quantity In Product Page Magento 2"
    url: "https://dolphinwebsolution.com/how-to-show-salable-quantity-in-product-page-magento-2"
---

# Models, Resource Models and Collections in Magento 2

_Published: March 27, 2021_  
_Author: Payal Patel_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/Models-1024x512.jpg)

> Models, Resource Models, and Collections 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.

In the previous tutorial, we learn how to create the table using the [Setup script](https://dolphinwebsolution.com/how-to-create-install-upgrade-uninstall-script-in-magento-2) and [Declarative Schema](https://dolphinwebsolution.com/declarative-schema-in-magento-2-3-x-or-later). 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 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](https://devdocs.magento.com/guides/v2.4/extension-dev-guide/view-models.html) **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.

### Resource Models in Magento 2
 Now, we are going to the resource model.

#### What is 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.

### Collections in Magento 2

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](https://dolphinwebsolution.com/shop/customer-attributes-for-magento.html) 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](https://dolphinwebsolution.com/how-to-perform-crud-operation-with-a-custom-module-in-magento-2).


---

_View the original post at: [https://dolphinwebsolution.com/models-resource-models-and-collections-in-magento-2](https://dolphinwebsolution.com/models-resource-models-and-collections-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:53:56 UTC_  
