Magento 2 Development Workshop Learn More

Magento 2 for Beginners

10 lessons

Model in Magento 2

Model in Magento 2

Model is one of the must have components for CRUD operations in Magento 2 framework. In this lesson, we are going to learn what is a model, what are the capabilities of the model and how to create a model and use it in your custom Magento 2 extension.

Lesson Overview

In this lesson we are going to learn the following:

  • What is a Model?
  • Model Capabilities
  • How to create a Model and use it in a Magento 2?

What is a Model in Magento 2?

A Model is a class that represents an entity or a record from a database. The model also provides access to entity data that is used across the Magento 2 application. The model term comes from a Model-View-Controller design pattern and is an important component to understand in Magento 2.

In order to create a model, a PHP class should extend an AbstractModel class, that comes from a Magento 2 framework. The model class inherits methods and logic that the AbstractModel class provides.

Let’s have a look at the PHP class called User. The user class extends an AbstractModel class and can be called a model.

use Magento\Framework\Model\AbstractModel;

class User extends AbstractModel
{    

}

In order to use the newly created User Model, we have to create a new instance of a User. A User Factory class creates a User instance, the setData method allows setting the value of “Max” to the key first name. The user object stores this data, and it can be accessed with the help of the getData method.

class Controller
{    
    // ...
    public function execute()
    {
         $user = $this->userFactory->create();
         $user->setData(‘first_name’, ‘Max’);

         $firstName = $user->getData(‘first_name’);
    }
}

As you noticed, we don’t use “new” operator in order to create a User object. This is because every model has a dependency that is injected into a construct method, and with the help of an Inversion of Control implemented in Magento 2, we can inject dependencies automatically.

For user objects to be created, we can use a UserFactory class that is automatically generated by Magento 2 framework at the time we've requested it in a construct method.

use MageMastery\ModelExample\Model\User;
use MageMastery\ModelExample\Model\UserFactory;

class Controller {   
    private $userFactory;

    public function __construct(UserFactory $userFactory)
    {
         $this->userFactory = $userFactory;
    }
    // ...
}

The User Factory class is also known as a Model Factory class with the single create method. The only responsibility of this auto-generated User Factory class is to create new instances of the User class.

Model Factory Class

Resource Model Relation

A model usually represents a business entity that is stored in a database. For the model to properly work, the so-called Resource Model class relation has to be provided inside the _construct() method.

use Magento\Framework\Model\AbstractModel;

class User extends AbstractModel
{    
     protected function _construct()
     {
          $this->_init(ResourceClassName::class);
     }
}

Don’t be confused with the public __construct() method. As this is a protected method with the single underscore. Inside this method, the _init method should be called with the name of the resource model class. We are going to talk about Resource Models in the next Mage Mastery lesson.

Optional Model Interfaces

A model can optionally implement interfaces for clarity and ease of use or to provide additional capabilities. The IdentityInterface interface can be added if the model needs to have a caching capability, let’s say, every time a model is saved or deleted, the cache for this model is cleared.

Model Optional Interfaces

Or, we can create a Data interface and provide public methods to be able to rely on interfaces rather than concrete implementations.

Model Class Location

Models are usually located inside the Model directory of a Magento 2 extension.

Model Location

Important

A model should be used when there is a need to retrieve data from a database and present it in the Magento 2 application. A model class represents a Domain Model of a Magento 2 extension.

The AbstractModel class has deprecated CRUD and other methods. These methods shouldn’t be used.

Deprecated Methods

Model is usually used in conjunction with a Resource Model class. This is the class that knows how to load and store data in a database. We will learn more about Resource Model in our next Mage Mastery lesson.