/   /   /  Magento 2 Concept : Model Resource DB and Collection (part2)

Note:

For more extensions and themes visit our store

Magento 2 Concept : Model Resource DB and Collection (part2)


In the first course of series we get quick concept of how the mechanism of Model Resource and db work , but almost is like Magento 1, in this course we introduce the collection and the new technique of service contract .

The collection is old notion of Magento , the resource use it for getting  a collection  (array of row normally based on filter) , if you go to class collection related to page Model in Magento\Cms\Model\ResourceModel\Page\Collection you finding in the folder vendor/module-cms/Model/ResourceModel/Page , the class extend from the parent, that exist in \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection yes from the Magento 2 framework :

  1.  
  2. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  3. {

In __construct of this parent class  :

  1.  
  2.  $this->_resource = $resource;
  3.  $this->setConnection($this->getResource()->getConnection());

as the simple resource but is have :
_initSelect() ,give you $this->getSelect()->from(['main_table' => $this->getMainTable()]); that equal select all rows from this flat table cms_page without any filter.

If you try to see the __construct  of Collection.php:

  1.  
  2.     protected function _construct()
  3.     {
  4.         $this->_init('Magento\Cms\Model\Page', 'Magento\Cms\Model\ResourceModel\Page');
  5.         $this->_map['fields']['page_id'] = 'main_table.page_id';
  6.         $this->_map['fields']['store'] = 'store_table.store_id';
  7.     }

Now the collection get all infos from the model page and resource ,when you get  the class of type collection with factory now you can start select and filter , some other functions to use :
join($table, $cond, $cols = '*') : for join this table with other table
save(): save all the entities in the collection
getAllIds() : Retrieve all ids for collection
toOptionIdArray() : change the result to array
addFieldToFilter : Add field filter to collection

ok all this article will be convert the practice in other part .

Now what is  service contract the simple definition :
A service contract must define data interfaces, which preserve data integrity, and service interfaces, which hide business logic from service requestors.

Generally gateway return information about data entities, return search results, and set validation rules and return validation results , about practice :
1 - Data interfaces
the model page implement Magento\Cms\Api\Data\PageInterface the interface  PageInterface have the complete meta data he preserve data integrity (the abstraction is required it's the role of interface ) how this ?

If you go to module Cms and open the di.xml inside etc you will find :

  1.  
  2. <preference for="Magento\Cms\Api\Data\PageInterface" type="Magento\Cms\Model\Page" />
  3.  

It's equal when every one want an instance of  PageInterface (normally you can't) give it an instance of model Page saw every other php application or any want the structure of data from this module the class reserved  is  PageInterface inside Api/Data in this module when this class  called give it an instance of  Magento\Cms\Model\Page and the relation is bidirectional  PageInterface preserve data integrity and  Page give ability of instance .

2 - Service interfaces
Magento 2 use the Repository for access to persistent data entities (service like delete save or getList)  Magento\Cms\Model\PageRepository implement PageRepositoryInterface inside folder Api , that just for reserve the data integrity and geteway, for the real PageRepository , PageRepository  the real server use the page model and resource (use  factory for get instance object) for  save and other operations but the client need use Api need pass by  PageRepositoryInterface and Magento with preference tag give it an instance of  PageRepository  in di.xml :

  1.  
  2. <preference for="Magento\Cms\Api\PageRepositoryInterface" type="Magento\Cms\Model\PageRepository" />
  3.  

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.