Models and Collections for Magento2 (Part : 3)

Creation of Models and Collections for Magento2 with real example

Posted on March 16, 2016 in Magento2, PHP

Up to this point, we have learned how to create a simple model, an EAV model, and install and upgrade types of schema and data script. Now, let us see how we can create, read, update and delete our entities, operations that are commonly referred to as CRUD.

In this tutorial, we first create a simple Test controller with the Test action we can trigger in the browser via a URL. Within this Test action, we will then dump our CRUD-related code.

First, we need to define a route to make Magento respond to the URL we provide in the browser. We do so by creating the app/code/Jeff/Office/etc/frontend/routes.xml file.


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
     <router id="standard">
          <route id="jeff_office" frontName="jeff_office">
                 <module name="Jeff_Office" />
          </route>
     </router>
</config>

Route definition requires a unique ID and frontName attribute values, which in our case both equal jeff_office. The URL formula for the test action goes like :

{magento_base_url}/{route frontName}/{controller name}/{action name}

Once the route has been defined, we can go ahead and create the Test controller, defined in the app/code/Jeff/Office/Controller/Test.php file with code as follows:


name Jeff\Office\Controller;

abstract class Test extends \Magento\Framework\App\Action\Action
{

}

This really is the simplest controller we could have defined. The only thing worth noting here is that the controller class needs to be defined as abstract and extend the \Magento\Framework\App\Action\Action class. Controller actions live outside of the controller itself and can be found under the subdirectory on the same level and named as controller. Since our controller is called Test, we place our Test action under the app/code/Jeff/Office/Controller/Test/Test.php file with content as follows:


namespace Jeff\Office\Controller\Test;

class Test extends \Jeff\Office\Controller\Test
{
    protected $employeeFactory;
    protected $departmentFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context;
        \Jeff\Office\Model\EmployeeFactory $employeeFactory,
        \Jeff\Office\Model\DepartmentFactory $departmentFactory
   ) 
   {
        $this->employeeFactory = $employeeFactory;
        $this->departmentFactory = $departmentFactory;
       return parent::__construct($context);
    }

   public function execute()
   {
        //Simple model, creating new entities.
        $department1 = $this->departmentFactory->create();
        $department1->setName('Finance');
        $department1->save();
        
       //eav model, creating new entities, flavor #1
       $employee1 = $this->employeeFactory->create();
       $employee1->setDepartmentId($department1->getId());
       $employee1->setEmail('dummy@example.com');
       $employee1->setFirstname('dummy');
       $employee1->setLastname('dummy');
       $employee1->setServiceYears(3);
       $employee1->setDob('2000-01-01');
       $employee1->setSalary(500.0(l
       $employee1->setVatNumber('GB123456');
       $employee1->save();
   }
}

That’s it for Magento2 model and collection. In this serial of tutorial, we create model, resourceModel, and collection for magento2. Further, we introduced the way to register new module and create route in the Magento2. Finally, we showed some codes for use the model and collection.

Thank you for your reading.


comments powered by Disqus