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 :
- class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
In __construct of this parent class :
- $this->_resource = $resource;
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:
- protected function _construct()
- $this->_init('Magento\Cms\Model\Page', 'Magento\Cms\Model\ResourceModel\Page');
- $this->_map['fields']['page_id'] = 'main_table.page_id';
- $this->_map['fields']['store'] = 'store_table.store_id';
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 :
- <preference for="Magento\Cms\Api\Data\PageInterface" type="Magento\Cms\Model\Page" />
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 :
- <preference for="Magento\Cms\Api\PageRepositoryInterface" type="Magento\Cms\Model\PageRepository" />