Magento 2 Create Model

Create Model in Magento 2 is a huge path of MVC architecture. In Magento 2 CRUD, models have many different functions such as manage data, install or upgrade module. In this tutorial, I only talk about data management CRUD. We have to create Model, Resource Model, Resource Model Conllection to manage data in table: mageplaza_topic as I mentioned above.

Before create model, we need to create the interface for it. Let create the TopicInterface:

app/code/Mageplaza/HelloWorld/Model/Api/Data/TopicInterface.php

And put this content:

<?php
namespace Mageplaza\HelloWorld\Model\Api\Data;
interface TopicInterface
{
	public function getId();
	public function setId();
	
	public function getTitle();
	public function setTitle();
	
	public function getContent();
	public function setContent();
	
	public function getCreationTime();
	public function setCreationTime();
}

This interface has defined the set and get method to table data which we would use when interacting with the model. This interface plays an important role when it comes time to exporting CRUD models to Magento service contracts based API.

Now we will create the model file:

app/code/Mageplaza/HelloWorld/Model/Topic.php

And this is the content of that file:

<?php 
namespace Mageplaza\HelloWorld\Model;
class Topic extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface,
\Mageplaza\HelloWorld\Model\Api\Data\TopicInterface
{
	const CACHE_TAG = 'mageplaza_topic';

	protected function _construct()
	{
		$this->_init('Mageplaza\HelloWorld\Model\ResourceModel\Topic');
	}

	public function getIdentities()
	{
		return [self::CACHE_TAG . '_' . $this->getId()];
	}
}

This model class will extends AbstractModel class Magento\Framework\Model\AbstractModel and implements TopicInterface and IdentityInterface \Magento\Framework\DataObject\IdentityInterface. The IdentityInterface will force Model class define the getIdentities() method which will return a unique id for the model. You must only use this interface if your model required cache refresh after database operation and render information to the frontend page.

The _construct() method will be called whenever a model is instantiated. Every CRUD model have to use the _construct() method to call _init() method. This _init() method will define the resource model which will actually fetch the information from the database. As above, we define the resource model Mageplaza\Topic\Model\ResourceModel\Topic The last thing about model is some variable which you should you in your model:

  • $_eventPrefix - a prefix for events to be triggered
  • $_eventObject - a object name when access in event
  • $_cacheTag - a unique identifier for use within caching

Related Post

Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
hello-world
create
model

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 create model
  • create model in magento 2
  • create model in magento 2
  • create model in magento 2 module
  • create custom model in magento 2
  • create resource model in magento 2
  • create custom source model in magento 2
  • create model in magento 2
  • create a custom model in magento 2
  • how to create custom model in magento 2
  • create new model in magento 2
  • model in magento 2 tutorial
  • 2.2.x, 2.3.x, 2.4.x