Create a Simple Model in Magento2

Department entity,as per requirements,is modeled as simple model.lets model class,resource class,add collection class form one unit.First we have to create model class,defined under the /app/code/Skynet/Office/Model/Deparment.php file as belows:-

**
 * User: Sumit Bera
 */

namespace Skynet\Office\Model;


use Magento\Framework\Model\AbstractModel;

class Department extends AbstractModel
{
    protected function  _construct()
    {
       $this->_init(\Skynet\Office\Model\ResourceModel\Photo::class);
    }
}

Extending from \Magento\Framework\Model\AbstractModel class and triggering the $this->_init method within _construct passing it to resource class.

https://www.amitbera.com/AbstractModel extends \Magento\Framework\Object.Model class ultimately extends from object means that we do not have to define property name in model class.Object enables us to get,set,unset,and check for value existence on properties.

Example:-

Lets imagine entity has a property calls employee_monthly_salary in following code:

$department->getData('employee_monthly_salary');
$department->getEmployeeMonthlySalary();

$department->setData('employee_monthly_salary','theValue');
$department->setEmployeeMonthlySalary('theValue');

$department->unsetData('employee_monthly_salary');
$department->unsEmployeeMonthlySalary();

$department->hasData('employee_monthly_salary');
$department->hasEmployeeMonthlySalary();

Object implementing getData,setData,unsetData and _call methods. A _call method implementation is that it understand method calls like getEmployeeMonthlySalary,setEmployeeMonthlySalary,unsEmployeeMonthlySalary and hasEmployeeMonthlySalary even if they not exists on the Model class.

After have model class,then create resource class in app/code/Skynet/Office/Model/ResourceModel/Deparment.php file as below:-
Create a Simple Model in Magento2After have model class,then create resource class in app/code/Skynet/Office/Model/ResourceModel/Deparment.php file as below:-

<?php
namespace Skynet/Office/Model/ResourceModel;


class Photo extends \Magento\Framework\Model\ResourceModel\Db\AbstractDbhttps://www.amitbera.com/
{
	
	public function __construct(
		\Magento\Framework\Model\ResourceModel\Db\Context $context
	)
	{
		parent::__construct($context);
	}
	
	protected function _construct()
	{
		$this->_init('skynet_offhttps://www.amitbera.com/ice_department_db', 'entity_id');
	}https://www.amitbera.com/
	
}

https://www.amitbera.com/resource class extends from \Magento\Framework\Model\ResourceModel\Db\AbstractDb triggers $this->_init method call with in _construct.$this->_init accept two parameters.First parameter is tabe name skynet_office_department_db,where model will persist its data.Second parameter is primary column name entity_id with in tht table.

https://www.amitbera.com/AbstractDb extends Magento\Framework\Model\ResourceModel\AbstractResource.Then we have to create Collection class in Skynet\Office\Model\ResourceModel\Deparment\Collection.php as follows:

<?php
/**
 * User: sumit
 */

namespace Skynet\Office\Model\ResourceModel\Deparment;


class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init(\Skynet\Office\Model\Deparment::class,
            \Skynet\Office\Model\ResourceModel\Deparment::class);
    }
}

https://www.amitbera.com/https://www.amitbera.com/collection class extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection as like model and resource class,$this->_init method call within _consrtuct._init accept two parameters.First parameter is full model class name Skynet\Office\Model\Deparment.Second parameter Skynet\Office\Model\ResourceModel\Deparment

AbstractCollection implements \Magento\Framework\App\ResourceCollection\SourcePrividerInterface,and extends \Magento\Framework\Data\Collection\AbstractDb.AbstractDb extends s \Magento\Framework\Data\Collection


Total Page Visits: 2626 - Today Page Visits: 5

Leave a Reply