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.

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.

Related Posts

Extend Magento 2 jQuery Widget

18 Jun 2019

|

Paul Mestereaga

In this short tutorial we will learn how to extend Magento 2 jQuery widget with our custom logic.

Console Commands in Magento 2

21 May 2019

|

Paul Mestereaga

In this tutorial I will give an overview of the console commands in Magento 2 and we will learn how to build a custom console command in Magento 2

Optimize Magento 2

Free guide to help you optimise your Magento 2 installation and improve your website speed

Get your free optimisation guide.

Enter your name and email to join my mailing list in exchange for the free download. You can opt out any time.


Can I email you?

I will use the information you provide on this form to stay in touch, send you updates, and send marketing emails about our products and services. You can unsubscribe at any time through a link in the footer of an email from me. For more information, please read the Privacy Policy.