This course of series give you the ability for creating your own model resource and collection , ok let's imagine you want create a simple module for persist some data like brand infos for example .
you can download file of this course and code from github
1 – Create :
First we need create our table inside our database for that you need create the Setup folder ,The path complete of module is Ibnab/Brand , inside Setup folder create file InstallSchema.php and fill with :
<?php namespace Ibnab\Brand\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; /** * @codeCoverageIgnore */ class InstallSchema implements InstallSchemaInterface { /** * {@inheritdoc} */ public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $installer->getConnection()->dropTable($installer->getTable('ibnab_brand')); $table = $installer->getConnection()->newTable( $installer->getTable('ibnab_brand') )->addColumn( 'brand_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 10, ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], 'Slider ID' )->addColumn( 'name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, ['nullable' => false, 'default' => ''], 'Brand title' )->addColumn( 'image', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, ['nullable' => true], 'Brand image' ); $installer->getConnection()->createTable($table); $installer->endSetup(); } }
Magento 2 use this class inside Setup for create all needed schema when you try to complete the installation of your module in first time with command in terminal: php bin/magento setup:upgrade
here we use resource for unit Setup inside framework magento2 it's give core_resource for write and read from db .
Here we used 2 cool function dropTable and newTable . The injected $setup object give you an adapter for using dropTable and newTable functions ( dropTable and newTable exist inside Magento\Framework\DB\Adapter\Pdo\Mysql).
Second we need create model The path complete of module is Ibnab/Brand , create the folder model and inside it create your model Brand.php , with content :
<?php namespace Ibnab\Brand\Model; class Brand extends \Magento\Framework\Model\AbstractModel { protected function _construct() { $this->_init('Ibnab\Brand\Model\ResourceModel\Brand'); } }
simple use case for more explain you need go to course Model Resource DB and Collection (part1) of this series
inside Ibnab/Brand/Model/ResourceModel create Brand.php (push):
<?php namespace Ibnab\Brand\Model\ResourceModel; class Brand extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb { protected function _construct() { $this->_init('ibnab_brand', 'brand_id'); } }
simple use case for more explain you need go to course Model Resource DB and Collection (part1) of this series
and inside Ibnab/Brand/Model/ResourceModel/Brand create Collection.php (push):
<?php namespace Ibnab\Brand\Model\ResourceModel\Brand; class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection { protected function _construct() { $this->_init('Ibnab\Brand\Model\Brand', 'Ibnab\Brand\Model\ResourceModel\Brand'); } }
simple use case for more explain you need go to course Model Resource DB and Collection (part2) of this series
2 – Use :
Ok let's imagine you are create simple controller inside Ibnab/Brand/Controller/Index/Index.php
(this controller is frontend type , for testing you need create the inside Ibnab/Brand/etc/frontend/routes.xml see inside the attachment file with this coure ) and the content is :
<?php namespace Ibnab\Brand\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { protected $_brandFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Ibnab\Brand\Model\BrandFactory $brandFactory ) { $this->_brandFactory = $brandFactory; parent::__construct($context); } public function execute() { /***create and save**/ $brandModel = $this->_brandFactory->create(); $brandModel->setName('Ibnab'); //or $brandModel->setData('name','Ibnab'); $brandModel->setImage('logo.png'); //or $brandModel->setData('image','logo.png'); $brandModel->save(); echo ""; $collection = $brandModel->getCollection(); foreach($collection as $brand): echo $brand->getName()."<br/ >"; echo $brand->getImage()."<br/ >"; endforeach; } }
use can inject your model in __construct of controller (action) , but not directly we use here technique of factory for building our object (we need obtain all dependency in life cycle of object),
ok inside main function (public function execute()) , we use function create of factory for getting model brand instance of Ibnab\Brand\Model\Brand , we can use directly setters with name of field of using the setData method (with setName we ufing the technique of magic method that take more time for compilation) , and you can use getters or directly with popular function getData.
You can get instance of class Ibnab\Brand\Model\ResourceModel\Brand\Collection with method $brandModel→getCollection(); (you can inject directly in __construct like model Ibnab\Brand\Model\ResourceModel\Brand\CollectionFactory and get the instance with function create ) , here we use method count and we using iterator for browse all brand every loop give you an instance type Ibnab\Brand\Model\Brand , with values of current row .
Comments