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

Note:

For more extensions and themes visit our store

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

We introduce in this series of courses the concept Model Resource  DB  and Collection in Magento2 , architecture of Magento 2 based on top of Magento 2 framework inside path vendor/framework it contain all class and global skeleton used and extended from all modules in Magento 2 .

Ok in this framework inside folder  vendor/framework/Model you find all parents and roots class of all models and resources of modules, Although the Magento 2 Framework does not contain resource models, it does contain a library of code to help implement a resource model.

We started with vendor/framework/Model AbstractModel.php  the abstract class contain all method and structure , it will be used from Model in the specific module

Example of flat model :

inside cms module you find the model Magento\Cms\Model\Page extend directly from AbstractModel :

  1.  
  2. class Page extends \Magento\Framework\Model\AbstractModel implements PageInterface, IdentityInterface
  3. {
  4.  
  5.  

that is for using the coming function and personalized function related to page model , if you scroll to __construct you will find :
  1.  
  2.    protected function _construct()
  3.     {
  4.         $this->_init('Magento\Cms\Model\Resource\Page');
  5.     }
  6.  
  7.  

cool it's use the parent method _init (_init is part of AbstractModel) :
  1.  
  2.     protected function _init($resourceModel)
  3.     {
  4.         $this->_setResourceModel($resourceModel);
  5.         $this->_idFieldName = $this->_getResource()->getIdFieldName();
  6.     }
  7.  
  8.  

and pass as argument the resource class  Magento\Cms\Model\ResourceModel\Page that class interact with db let's discover :
A -  the page resource extends from Magento\Framework\Model\ResourceModel\Db\AbstractDb
  1.  
  2. class Page extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  3. {
  4.  
  5.  

and AbstractDb extends from Magento\Framework\Model\ResourceModel\AbstractResource
  1.  
  2. abstract class AbstractDb extends AbstractResource
  3. {
  4.  
  5.  

as you see the resource page  extends  the resource in the Model component of framework Magento 2  :
the page resource in construct use

  1.  
  2.     protected function _construct()
  3.     {
  4.         $this->_init('cms_page', 'page_id');
  5.     }
  6.  
  7.  

it use the __init mehtod of class parent AbstactDb for put the name of table related to model page (the response of from where I get the rows with resource for model page ?)
  1.  
  2.     protected function _init($mainTable, $idFieldName)
  3.     {
  4.         $this->_setMainTable($mainTable, $idFieldName);
  5.     }
  6.  
  7.  

this method add the table Suffix  (and small play with cache  is not in our subject).


Ok I like now go to method construct of class  AbstactDb :

  1.  
  2.    public function __construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, $resourcePrefix = null)
  3.     {
  4.         $this->transactionManager = $context->getTransactionManager();
  5.         $this->_resources = $context->getResources();
  6.         $this->objectRelationProcessor = $context->getObjectRelationProcessor();
  7.         if ($resourcePrefix !== null) {
  8.             $this->_resourcePrefix = $resourcePrefix;
  9.         }
  10.         parent::__construct();
  11.     }
  12.  
  13.  

As you can see $this->_resources = $context->getResources();  the object Resource is instance of Magento\Framework\App\ResourceConnection , Magento 2 put with di this Object give you the ability of row sql and other choice , it give you the connection and from connection you can get Adapter for writng and reading ….


AbstactDb get the object Magento\Framework\App\ResourceConnection  from it he get the connections and from connections with method _getReadAdapter() and _getWriteAdapter() can use many  methods charged with select or delete or update or save , because the _getReadAdapter() and _getWriteAdapter()  is retourn a object of type Magento\Framework\DB\Adapter\Pdo\Mysql ..

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.

Newsletter