Storing data in a database table is handled through a Model class; this model holds the data. While saving the data, a ResourceModel is used, and this class is the link between the Model and database table, and all CRUD operations go through the ResourceModel. When loading a set of records, a Collection is used; it is possible to apply filters to this collection.
Using database models requires that the module configuration is done correctly; otherwise, the autoloader won't be able to find the files to load.
The following steps in this recipe will add the database models to your module:
Model class:Model/Demo.php
<?php namespace Genmato\Sample\Model; use Magento\Framework\Model\AbstractModel; class Demo extends AbstractModel { /** * Initialize resource model * @return void */ protected function _construct() { $this->_init('Genmato\Sample\Model\ResourceModel\Demo'); } }
ResourceModel class:Model/ResourceModel/Demo.php
<?php namespace Genmato\Sample\Model\ResourceModel; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; class Demo extends AbstractDb { /** * Initialize resource model * @return void */ protected function _construct() { $this->_init('genmato_demo', 'demo_id'); } }
Collection class:Model/ResourceModel/Demo/Collection.php
<?php namespace Genmato\Sample\Model\ResourceModel\Demo; use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; class Collection extends AbstractCollection { /** * @var string */ protected $_idFieldName = 'demo_id'; /** * Define resource model * @return void */ protected function _construct() { $this->_init('Genmato\Sample\Model\Demo', 'Genmato\Sample\Model\ResourceModel\Demo'); } }
The Model class specifies the used ResourceModel in the constructor through the _init() function. When the save() function is called on a Model, it will call the save() function on the ResourceModel; see the save() function in AbstractModel that is extended:
/**
* Save object data
*
* @return $this
* @throws \Exception
*/
public function save()
{
$this->_getResource()->save($this);
return $this;
}Here, _getResource() returns an instance of the class specified in the _init() function.
In the ResourceModel class constructor, the _init() function is called to specify the genmato_demo database table and primary key in the demo_id table. This will be used when creating the queries necessary to load or save the data. Depending on the action performed (save new, save existing, or delete), a corresponding query is generated using an INSERT, UPDATE, or DELETE query. This is handled by the framework and is built (currently) on the Zend_Db_Adapter_Pdo_Mysql class.
In the Collection class, both Model and ResourceModel are specified in the _init() function. ResourceModel is necessary to connect to the database and load the records from the right database table, allowing you to use filters to select the records necessary. The collection is then represented as an array of Models to allow all functionality available to models (magic getters/setters, adding data, and delete/save).