In the previous commodity you learned how to create new tables in Magento two database. In this one, you volition acquire more than about the models used to work with the Magento 2 database which allows you lot to read, edit and delete the data.

Magento 2 and Magento 1 uses the Model/ResourceModel/Collection ORM (Object-relational mapping) for these purposes. To implement this concept you lot demand to create three files (model, resource model, collection).

1. Create the Model file:

app/code/VendorName/ModuleName/Model/Somemodel.php

add the following code to it:

<?php
namespace VendorName\ModuleName\Model;
class Somemodel extends \Magento\Framework\Model\AbstractModel
{
    protected function _construct()
    {
        $this->_init('VendorName\ModuleName\Model\ResourceModel\Somemodel');
    }
}

The _construct initializes the resource model. Pass the proper name of the resources model form (create it in the side by side footstep) to the _init method.

Model is what yous will most frequently work with. This class must exist inherited from\Magento\Framework\Model\AbstractModel or its child models. It is responsible for working on one record in the table. The about commonly used methods in this class are:

Load methods:

          load($id)          - loads an entry whose primary key equals $id;
load($value, $columnName) - loads an entry whose cavalcade value $columnName equals $value;

Getters and setters methods:

          getSomeKey()          - gets the value for the holding (cavalcade) some_key;
getData('some_key') - analog of getSomeKey;
setSomeKey($value) - sets the value for the property (column) some_key;
setData('some_key', $value) - analog of setSomeKey;
getId() - gets the value of the primary key;
setId($id) - set the value of the main primal;

Saving and deleting methods:

          save()          - saves data to the database. If the primary key is not specified, creates a new entry;
delete() - deletes an entry whose key matches the central of the model object;

Other methods:

          getCollection()          - returns the collection object;
getResource() - returns the resource model object;

You tin can learn nearly additional methods by exploring the following classes:
\Magento\Framework\Model\AbstractModel
\Magento\Framework\DataObject

2. Create the Resources Model class:

app/lawmaking/VendorName/ModuleName/Model/ResourceModel/Somemodel.php

add the following code to it:

<?php
namespace VendorName\ModuleName\Model\ResourceModel;
class Somemodel extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    protected part _construct()
    {         $this->_init('table_name_without_prefix', 'primary_column_name');     }
}

table_name_without_prefix - the table proper noun in the Magento ii database without prefix;
primary_column_name - the name of the field in this table with the main key and autoincrement.

The loading and saving methods in the model work with the database through the resources model.

3. Create Collection file:

app/code/VendorName/ModuleName/Model/ResourceModel/Somemodel/Collection.php

add the following code to it:

<?php
namespace VendorName\ModuleName\Model\ResourceModel\Somemodel;
grade Collection
    extends \Magento\Framework\Model\ResourceModel\Db\Drove\AbstractCollection
{
    protected function _construct()
    {         parent::_construct();
        $this->_init(
            'VendorName\ModuleName\Model\Somemodel',
            'VendorName\ModuleName\Model\ResourceModel\Somemodel'
        );     }
}

You lot must pass model and resources model class names to the _init method.

The collection is responsible for downloading several entries from the table. The virtually usually used methods in this course are:

          addFieldToFilter($field, $condition)          - adds "WHERE $ field = $ condition" constraint;
addFieldToSelect($field) - defines which columns to include in the query. By default, all columns are included in the query;
setOrder($field, $direction) - indicates "ORDER Past $ field $ direction" sorting;
setPageSize($size) - indicates the "LIMIT $ size" restriction;
setCurPage($folio) - sets the page number;

Learn near other methods past exploring the following classes:
\Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
\Magento\Framework\Data\Drove\AbstractDb
\Magento\Framework\Information\Collection

Instance of working with a collection:

$collection = $this->collectionFactory->create();
$collection->addFieldToFilter('status', 1)
    ->addFieldToFilter('publish_date', ['lteq' => '2017-11-01'])
    ->setPageSize(10);
foreach ($collection as $item) {
    echo $item->getName();
}

This code executes queries to the database and receives the first ten entries, in which the status column equals 1 and publish_date is less than or equal to 2017-11-01. Then, this code displays the value of the 'proper noun' column for each received entry in a loop.

In the examples, we develop a module for FAQs. You can view the inverse lawmaking on GitHub.