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 :
class Page extends \Magento\Framework\Model\AbstractModel implements PageInterface, IdentityInterface {
that is for using the coming function and personalized function related to page model , if you scroll to __construct you will find :
protected function _construct() { $this->_init('Magento\Cms\Model\Resource\Page'); }
cool it's use the parent method _init (_init is part of AbstractModel) :
protected function _init($resourceModel) { $this->_setResourceModel($resourceModel); $this->_idFieldName = $this->_getResource()->getIdFieldName(); }
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
class Page extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb {
and AbstractDb extends from Magento\Framework\Model\ResourceModel\AbstractResource
abstract class AbstractDb extends AbstractResource {
as you see the resource page extends the resource in the Model component of framework Magento 2 :
the page resource in construct use
protected function _construct() { $this->_init('cms_page', 'page_id'); }
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 ?)
protected function _init($mainTable, $idFieldName) { $this->_setMainTable($mainTable, $idFieldName); }
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 :
public function __construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, $resourcePrefix = null) { $this->transactionManager = $context->getTransactionManager(); $this->_resources = $context->getResources(); $this->objectRelationProcessor = $context->getObjectRelationProcessor(); if ($resourcePrefix !== null) { $this->_resourcePrefix = $resourcePrefix; } parent::__construct(); }
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