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:

  1. Create the Model class:

    Model/Demo.php

    <?php
    namespace Genmato\Sample\Model; use Magento\Framework\Model\AbstractModel; ...

Get Magento 2 - Build World-Class online stores now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.