Magento 2 Training : Unit 3 – Lesson B

Models declaration

For our first model creation, let’s begin with the “Department” model. On this part, we will create a simple model, without

We define :
– An event prefix ($_eventPrefix)
– An object name for the events ($_eventObject)
– An ID field name ($_idFieldName)

So create the file :
app/code/Maxime/Jobs/Model/Department.php

Our class will have the following content :

<?php
namespace Maxime\Jobs\Model;

use \Magento\Framework\Model\AbstractModel;

class Department extends AbstractModel
{
    const DEPARTMENT_ID = 'entity_id'; // We define the id fieldname

    /**
     * Prefix of model events names
     *
     * @var string
     */
    protected $_eventPrefix = 'jobs'; // parent value is 'core_abstract'

    /**
     * Name of the event object
     *
     * @var string
     */
    protected $_eventObject = 'department'; // parent value is 'object'

    /**
     * Name of object id field
     *
     * @var string
     */
    protected $_idFieldName = self::DEPARTMENT_ID; // parent value is 'id'

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Maxime\Jobs\Model\ResourceModel\Department');
    }

}

On the construct method, we declare a ResourceModel path. We will create it below.

Before, we create the “Job” model :
app/code/Maxime/Jobs/Model/Job.php

With the content :

<?php
namespace Maxime\Jobs\Model;

use \Magento\Framework\Model\AbstractModel;

class Job extends AbstractModel
{
    const JOB_ID = 'entity_id'; // We define the id fieldname

    /**
     * Prefix of model events names
     *
     * @var string
     */
    protected $_eventPrefix = 'jobs';

    /**
     * Name of the event object
     *
     * @var string
     */
    protected $_eventObject = 'job';

    /**
     * Name of object id field
     *
     * @var string
     */
    protected $_idFieldName = self::JOB_ID;

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Maxime\Jobs\Model\ResourceModel\Job');
    }
}

ResourceModels declaration

We declared two resourcemodels on the previous step, so we must create its !

Add the file :
app/code/Maxime/Jobs/Model/ResourceModel/Department.php

And put this content :

<?php
namespace Maxime\Jobs\Model\ResourceModel;

use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;

/**
 * Department post mysql resource
 */
class Department extends AbstractDb
{

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        // Table Name and Primary Key column
        $this->_init('maxime_department', 'entity_id');
    }

}

Few informations here.
Another construct method, with two parameters here :
– Table name
– Primary column’s name

Let’s do the same for Job :
app/code/Maxime/Jobs/Model/ResourceModel/Job.php

With content :

<?php
namespace Maxime\Jobs\Model\ResourceModel;

use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;

/**
 * Department post mysql resource
 */
class Job extends AbstractDb
{

    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        // Table Name and Primary Key column
        $this->_init('maxime_job', 'entity_id');
    }

}

It’s the same structure as the department file.
You can check the Magento class named “AbstractDb” to see it’s content and learn some native Magento 2 methods.

Collections class creation

The last step before playing with our models is to create collection classes, to manipulation some sorts, limits, filters etc…

We create the collection class for departments :
app/code/Maxime/Jobs/Model/ResourceModel/Department/Collection.php

Which contains :

<?php
namespace Maxime\Jobs\Model\ResourceModel\Department;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{

    protected $_idFieldName = \Maxime\Jobs\Model\Department::DEPARTMENT_ID;
    
    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Maxime\Jobs\Model\Department', 'Maxime\Jobs\Model\ResourceModel\Department');
    }

}

As you can see, the “init” method has got two paramaters :
– The model path
– The ResourceModel path

And make the same thing for jobs :
app/code/Maxime/Jobs/Model/ResourceModel/Job/Collection.php

With :

<?php
namespace Maxime\Jobs\Model\ResourceModel\Job;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{

    protected $_idFieldName = \Maxime\Jobs\Model\Job::JOB_ID;

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Maxime\Jobs\Model\Job', 'Maxime\Jobs\Model\ResourceModel\Job');
    }

}

For your knowledge, you can check the Magento “AbstractCollection” class in order to see it’s methods 😉

These models allow you to manipulate “Job” and “Department” objects !
We will do that on the next lesson !

Continue training
Return to previous lesson
Create a model with Magento 2
Tagged on:         

2 thoughts on “Create a model with Magento 2

  • 07/18/2017 at 19:41
    Permalink

    great post!!!
    Im mexican magento 2 frontend devoleper and im looking for a admin grid tutorial and this is exellente

    Reply
  • 05/07/2018 at 09:25
    Permalink

    Hi
    Can you have any zip Maxime_Jobs module so we will get the proper idea

    Reply

Leave a Reply to vishal Cancel reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website.
Ok