Models and Collections for Magento2 (Part : 1)

Creation of Models and Collections for Magento2 with real example

Posted on March 16, 2016 in Magento2, PHP

Like most modern frameworks and platforms, these days Magento embraces an Object Relational Mapping(ORM) approach over raw SQL queries.Though the underlying mechanism still comes down to SQL, we are now dealing strictly with Objects.

For the purpose of this tutorial, we will create a miniature module called Jeff_Office.

The Module will have two entities defined as follows:

  • Department: a simple model with the following fields:
    • entity_id:primary key
    • name: name of department, string value
  • Employee: an EAV model with following fields and attributes:
    • Fields
      • entity_id: primary key
      • department_id: foreign key, pointing to Department.entity_id
      • email: unique e-mail of an employee, string value
      • first_name: first name of an employee, string value
      • last_name: last name of an employee, string value
    • Attributes:
      • service_year: employee’s years of service, integer value
      • dob: employee’s data of birth, datetime value
      • salary: decimal value
      • var_number: string value
      • note: possible note on employee, string value

Every module starts with the registration.php and module.xml files.


<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Jeff_Office" setup_version="1.0.0">
              <module name="Magento_Eav" />

The value of setup_version is important because we might use it within our schema install script (InstallSchema.php) files, effectly turning the install script into update script.

The sequence element is Magento2’s way of setting dependencis for our module.

First Part: simple model class

Let’s start by first creating a model class, defined under the app/code/Jeff/Office/Model/Department.php file as follows:

namespace Jeff\Office\Model;

class Department extends \Magento\Framework\Model\AbstractModel
    protected function _construct() 

Once we have a model class in place, we create a model resource class, defined under the app/code/Jeff/Office/Model/ResourceModel/Department.php file as follows:

namespace Jeff\Office\Model\ResourceModel;

class Department extends \Magento\Framewwork\Model\ResourceModel\Db\AbstractDb
    protected function _construct()
        $this->_init('jeff_office_department', 'entity_id');

The resource class is the key to communicating to the database. All it takes is for us to name the table and its primary key and our models can save, delete and update entities.

Finally, we create our collection class defined under the app/code/Jeff/Office/Model/ResourceModel/Department/Collection.php file.

namespace Jeff\Office\Model\ResourceModel\Department;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
    protected function _contruct()
         $this->_init('Jeff\Office\Model\Department', 'Jeff\Office\Model\ResourceModel\Department');

The _init function accepts two parameters. The first parameter is the full model class name, the second parameter is the full resource class name.

Second Part: EAV model class

The Employee entity, as per requirements, is modeled as an EAV model.

Let’s first start by creating EAV model class,


namespace Jeff\Office\Model;

class Employee extends \Magento\Framework\Model\AbstractModel
    const ENTITY = 'jeff_office_employee';

    public function _construct()

The EAV Model is the same as with the simple model previously described. They both extend from the \Magento\Framework\Model\AbstractModel class.

Next, we create the EAV model resource class.


namespace Jeff\Office\Model\ResourceModel;
class Employee extends \Magento\Eav\Model\Entity\AbstractEntity
    protected function _construct()
         $this->_read = 'jeff_office_employee_read';
         $this->_write = 'jeff_office_employee_write';

    public function getEntityType()
        if (empty($this->_type)) {

        return parent::getEntityType();

The resource class extends from \Magento\Eav\Model\Entity\AbstractEntity, and sets the $this->_read, $this->_write class properties through _construct. The read and write connections need to be naemd or else Magento produces an error when using our entity.

Finally, we create the collection class as follows:

namespace Jeff\Office\Model\ResourceModel\Employee;

class Collection extends \Magento\Eav\Model\Entity\Collection\AbstractCollection
    protected function _construct()
        $this->_init('Jeff\Office\Model\Employee', 'Jeff\Office\Model\ResourceModel\Employee');

As we can see, EAV models look a lot like simple models. The difference lies mostly in the resource class and collection class implementations and their first level parent class.

This is the first part of Models and Collections for Magento2 implementation. Please read the next tutorial by clicking Here.

comments powered by Disqus