Models in Magento 2 - Tutorial Blog - Paul Meștereagă
Magento 2

Models in Magento 2

Learn how to create a Models in Magento 2. Create Models, Resource Models and a Collections in Magento 2 step by step. Get code examples and code source of the plugin.

author

Paul Mestereaga

January 19, 2018

Let’s continue our series with a tutorial about Models in Magento 2. You can check my previous posts about Magento 2.

First of all Models we store the code in Model folder of our project. The most basic entity in this namespace is ResourceModel because it’s communicating directly with the database.

Resource Models

Resource Models are stored in Model/ResourceModel folder of our project. Here we create our file Item.php and implement the Item class that extends AbstractDb. It’s mandatory to implement the constructor of the class. Here we define the table to which the resource is connected.

<?php
namespace Mesteru\FirstModule\Model\ResourceModel;
 
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
 
class Item extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('mesteru_sample_item', 'id');
    }
}

Models

Now that we have the Resource Model we create our Model class. In the Model folder we create the file Item.php where we define the Item model class that extends AbstractModel class. In this class it’s also mandatory to implement the constructor where we call the _init method giving our Resource Model as parameter.

<?php
namespace Mesteru\FirstModule\Model;
 
use Magento\Framework\Model\AbstractModel;
 
class Item extends AbstractModel
{
    protected $_eventPrefix = 'mesteru_sample_item';
 
    protected function _construct()
    {
        $this->_init(\Mesteru\FirstModule\Model\ResourceModel\Item::class);
    }
}

Collections

Now lets create a Collection, which is actually a Resource Model. Because of that we create the file Collection.php in the ResourceModel/Item folder. We define the Collection class that extends AbstractCollection. For this class we need to define the $_idFieldName property. In the constructor we call the _init method giving as parameters our Model and Resource Model.

<?php
namespace Mesteru\FirstModule\Model\ResourceModel\Item;
 
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Mesteru\FirstModule\Model\Item;
use Mesteru\FirstModule\Model\ResourceModel\Item as ItemResource;
 
class Collection extends AbstractCollection
{
    protected $_idFieldName = 'id';
 
    protected function _construct()
    {
        $this->_init(Item::class, ItemResource::class);
    }
}

Conclusion

This is how we create Models in Magento 2. Now the data is called from our mesteru_sample_item table by our Resource Model and we are able to create Models that are using the Resource Model to call or write data and access Collections.

Get the Code

You can get the complete code on my github profile here.

Magento 2 from Zero to Beginner Free Online Course

In 10 hours you will have all the basic knowledge so that you could develop your modules without wandering around how to implement stuff.

Related Posts

Magento checkout optimization

15 Dec 2022

|

Paul Mestereaga

Optimizing the checkout process on your Magento eCommerce website is essential for increasing conversions and improving customer satisfaction. A seamless checkout experience can make all the difference in the success of your online store.

3 Things That Kill Your Magento Store’s Conversion Rate

10 Dec 2021

|

Paul Mestereaga

The store receives a lot of traffic, and you market to the right audience. The brand is actively promoted on social media and succeeds in SEO, so the marketing efforts appear to be working.
But what if it doesn’t lead to many sales?

optimize-magento-preview

FREE!

Magento 2
Optimization guide

Wondered how you can make your Magento load faster? Wonder no more. Here is a step by step guide that helps you.