We will create Blog.php under Model Folder
The model class will extend \M
agento\Framework\Model\AbstractModel
and implements \Magento\Framework\DataObject\IdentityInterface
. The IdentityInterface will force the Model class to define the getIdentities()
method which will return a unique id for each model.
Resource Model
So we can create Resource model folder and Blog.php under it.
Collection
We understand that in the context of database tables, a model represents an individual row, while a collection encompasses all the rows within that table. You can envision the collection as a group of models or equivalently, as executing a "Select * From Table table_name" query.
When we need to work with multiple rows, such as retrieving all records, applying filters using "where" or "like" clauses, or sorting with "order by", we utilize a collection.
On the other hand, if our task involves inserting a single row, fetching a specific row, updating a column in a row, or deleting a single row, then we employ a model.
Now, let's proceed to create a collection class.
Here we have to mention our table name and the primary key of the table. All the resource model classes will extend \Magento\Framework\Model\ResourceModel\Db\AbstractDb class which have the common functions for actual database operations.
Collection.php
Here we have to provide both the model and the resource model when we call the init method in the constructor.