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 GenmatoSampleModel;
    use MagentoFrameworkModelAbstractModel;
    class Demo extends AbstractModel
    {
      /**
      * Initialize resource model
      * @return void
      */
      protected function _construct()
      {
        $this->_init('GenmatoSampleModelResourceModelDemo');
      }
    }
  2. Create the ResourceModel class:

    Model/ResourceModel/Demo.php

    <?php
    namespace GenmatoSampleModelResourceModel;
    use MagentoFrameworkModelResourceModelDbAbstractDb;
    class Demo extends AbstractDb
    {
      /**
      * Initialize resource model
      * @return void
      */
      protected function _construct()
      {
        $this->_init('genmato_demo', 'demo_id');
      }
    }
  3. Create the Collection class:

    Model/ResourceModel/Demo/Collection.php

    <?php
    namespace GenmatoSampleModelResourceModelDemo;
    use MagentoFrameworkModelResourceModelDbCollectionAbstractCollection;
    class Collection extends AbstractCollection
    {
      /**
      * @var string
      */
      protected $_idFieldName = 'demo_id';
    
      /**
      * Define resource model
      * @return void
      */
      protected function _construct()
      {
        $this->_init('GenmatoSampleModelDemo', 'GenmatoSampleModelResourceModelDemo');
      }
    }

How it works…

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).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.249.42