Working with database models
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.
Getting ready
Using database models requires that the module configuration is done correctly; otherwise, the autoloader won't be able to find the files to load.
How to do it…
The following steps in this recipe will add the database models to your module:
Create the
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'); } }
Create the
ResourceModel
class:Model...