Steps to Create Model, Resource Model, and Collection in Magento 2

Hello Everyone!

Here in this blog we will learn how to create Model, Resource Model and Collection in Magento.

Before moving towards Models firstly we have to create a table in database. For that we need to create db_schema.xml file inside etc folder.

Step 1: Create a vendor folder inside app/code and inside Vendor’s folder create a News folder

Folder: app/code/Sample/News

Step 2: Now inside News folder create an etc folder and inside etc folder create db_schema.xml file and write the below given code

File:- app/code/Sample/News/etc/db_schema.xml

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="news_table">
    <column xsi:type="int" name="news_id" padding="10" unsigned="true" nullable="false" identity="true" comment="News Id"/>
    <column xsi:type="varchar" name="name" padding="10" unsigned="true" nullable="false" comment="Name" />
    <column xsi:type="varchar" name="description" padding="10" unsigned="true" nullable="false" lenght="1000" comment="Description" />
    <column xsi:type="int" name="is_active" padding="10" unsigned="true" nullable="false" comment="status"/>
    <column xsi:type="datetime" name="created_at" default="CURRENT_TIMESTAMP" on_update="false" nullable="true" comment="Creation date" />
    <column xsi:type="datetime" name="updated_at" default="CURRENT_TIMESTAMP" on_update="true" nullable="true" comment="Updation date"/>
    <constraint xsi:type="primary" referenceId="PRIMARY">
        <column name="news_id"/> 
    </constraint>
</table>
</schema>

STEP 3: Now inside Model folder create a News.php file

app/code/Sample/News/ Model/News.php

After creating the file write the below given code

<?php

namespace Sample\News\Model;

use Sample\News\Model\ResourceModel\News as ResourceModel;
use Magento\Framework\Model\AbstractModel;
class News extends AbstractModel
{
      protected function _construct()
      {
           $this->_init(NewsResourceModel::class);
      }
}

STEP 4: Now inside Model Folder create another folder ResourceModel and then inside ResourceModel create a file News.php

app/code/Sample/News/Model/ResourceModel/News.php

<?php 
namespace Sample\News\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class News extends AbstractDb
{
     protected function _construct()
     {
          $this->_init('news_table', 'news_id');
     }
}

STEP 5: Inside ResourceModel create another News folder and then inside News folder create Collection.php file.

app/code/Sample/News/Model/ResourceModel/News/Collection.php

namespace Sample\News\Model\ResourceModel\News;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Sample\News\Model\News as Model;
use Sample\News\Model\ResourceModel\News as ResourceModel;


class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init(Model::class, ResourceModel::class);
    }
    
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.