Create a Simple Model in Magento2
Department entity
**
* 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:-
$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
After
Create a Simple Model in Magento2After
<?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
AbstractCollection implements \Magento\Framework\App\ResourceCollection\SourcePrividerInterface,and extends \Magento\Framework\Data\Collection\AbstractDb.AbstractDb extends s \Magento\Framework\Data\Collection