
Meaning collections are a group of models. CollectionĬollections are used when we want to fetch multiple rows from our table. The name of the table in the database, and the name of the primary column in that table. Magento\Framework\Model\ResourceModel\Db\AbstractDbclass.Īpp/code/Jayanka/HelloWorld/Model/ResourceModel/Car.php _init('my_cars', 'car_id') Īs we can see, the here also has one method,, where we call the, and pass two parameters to it. Every model must have a resource model, since all of the methods of a resource model expects a model as its first parameter. But what is a resource model? Resource ModelĪll of the actual database operations are executed by the resource model. The Carclass only has one method, _construct(), when we call the _init()method, and pass the resource model’s name as its parameter.

To understand how data objects work, you can read the tutorial on data objects here.Īpp/code/Jayanka/HelloWorld/Model/Car.php _init(ResourceModel::class) Every model extends the Magento\Framework\Model\AbstractModelclass, which inherits the \Magento\Framework\DataObjectclass, hence, we can call the setDataand getData functions on our model, to get or set the data of a model respectively. every table we create in our database) should have its own model class. As a rule of thumb, every entity we create (i.e.

The fetching, extraction, and manipulation of data occur through models.

Models are like a black box which provides a layer of abstraction on top of the resource models. If you wanna follow along, first you’ll have to create a simple hello world module, and then create a table called “my_cars” containing the columns “car_id”, “manufacturer”, and “model”, using an Install Schema setup script. In this tutorial, we will be discussing each component individually, and then use all three to make a simple module. Magento’s “Model system” is divided into three parts – models, resource models, and collections. Models are used to do data operations, namely Create, Read, Update and Delete, on a database. Models in Magento are an inherent part of the MVC (Model-View-Controller) architecture.
