Created
May 5, 2017 07:59
-
-
Save softtama/a20e1d7410f7a05213bb3f4b3e17d5e5 to your computer and use it in GitHub Desktop.
Magento 2 Model, Resource Model, Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Rizki Pratama @ciptaloka.com | |
* @copyright Copyright (c) 2017 Ciptaloka (http://ciptaloka.com) | |
* @package Ciptaloka_HelloWorld | |
*/ | |
namespace Ciptaloka\HelloWorld\Block\Catalog\Product; | |
use \Magento\Framework\View\Element\Template; | |
use \Magento\Framework\View\Element\Template\Context; | |
use \Magento\Framework\ObjectManagerInterface; | |
use \Ciptaloka\HelloWorld\Helper\ConfigurationHelper; | |
class HelloWorld extends Template | |
{ | |
/** | |
* @var ConfigurationHelper | |
*/ | |
protected $_helper; | |
/** | |
* @var ObjectManagerInterface | |
*/ | |
protected $_objectManager; | |
public function __construct( | |
Context $context, | |
array $data = [], | |
ConfigurationHelper $helper, | |
ObjectManagerInterface $objectManager | |
) { | |
parent::__construct($context, $data); | |
$this->_helper = $helper; | |
$this->_objectManager = $objectManager; | |
} | |
/** | |
* Returns 'Hello World!' string | |
* | |
* @return \Magento\Framework\Phrase | |
*/ | |
public function getHelloWorldTxt() | |
{ | |
return __('Hello World!'); | |
} | |
public function getBlockLabel() | |
{ | |
return $this->_helper->getBlockLabel(); | |
} | |
public function getTextAlign() | |
{ | |
return $this->_helper->getTextAlign(); | |
} | |
/** | |
* Proceed return the block output if the `Enable Hello World` | |
* configuration is set to `Yes`, otherwise just return empty string. | |
* | |
* @return string | |
*/ | |
protected function _toHtml() | |
{ | |
if ($this->_helper->getEnable()) { | |
return parent::_toHtml(); | |
} | |
else { | |
return ''; | |
} | |
} | |
public function getCollection() | |
{ | |
$model = $this->_objectManager->create('Ciptaloka\HelloWorld\Model\HelloWorld'); | |
$collection = $model->getCollection(); | |
return $collection; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Rizki Pratama @ciptaloka.com | |
* @copyright Copyright (c) 2017 Ciptaloka (http://ciptaloka.com) | |
* @package Ciptaloka_HelloWorld | |
*/ | |
namespace Ciptloka\HelloWorld\Model; | |
use Magento\Framework\Model\AbstractModel; | |
use Magento\Framework\Model\Context; | |
use Magento\Framework\Model\ResourceModel\AbstractResource; | |
use Magento\Framework\Data\Collection\AbstractDb; | |
use Magento\Framework\Registry; | |
class HelloWorld extends AbstractModel | |
{ | |
/** | |
* Parent constructor | |
* | |
* @param Context $context | |
* @param Registry $registry | |
* @param AbstractResource|null $resource | |
* @param AbstractDb|null $resourceCollection | |
* @param array $data | |
*/ | |
public function __construct( | |
Context $context, | |
Registry $registry, | |
AbstractResource $resource = null, | |
AbstractDb $resourceCollection = null, | |
array $data = [] | |
) { | |
parent::__construct($context, $registry, $resource, $resourceCollection, $data); | |
} | |
/** | |
* Model constructor | |
*/ | |
protected function _construct() | |
{ | |
$this->_init('Ciptaloka\HelloWorld\Model\ResourceModel\HelloWorld'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Rizki Pratama @ciptaloka.com | |
* @copyright Copyright (c) 2017 Ciptaloka (http://ciptaloka.com) | |
* @package Ciptaloka_HelloWorld | |
*/ | |
namespace Ciptaloka\HelloWorld\Model\ResourceModel; | |
use Magento\Framework\Model\ResourceModel\Db\AbstractDb; | |
class HelloWorld extends AbstractDb | |
{ | |
/** | |
* Model initialization | |
* | |
* @return void | |
*/ | |
protected function _construct() | |
{ | |
$this->_init('ciptaloka_helloworld', 'id'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Rizki Pratama @ciptaloka.com | |
* @copyright Copyright (c) 2017 Ciptaloka (http://ciptaloka.com) | |
* @package Ciptaloka_HelloWorld | |
*/ | |
namespace Ciptaloka\HelloWorld\Model\ResourceModel\HelloWorld; | |
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; | |
class Collection extends AbstractCollection | |
{ | |
/** | |
* Collection constructor | |
*/ | |
protected function _construct() | |
{ | |
$this->_init( | |
'Ciptaloka\HelloWorld\Model\HelloWorld', | |
'Ciptaloka\HelloWorld\Model\ResourceModel\HelloWorld' | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Rizki Pratama @ciptaloka.com | |
* @copyright Copyright (c) 2017 Ciptaloka (http://ciptaloka.com) | |
* @package Ciptaloka_HelloWorld | |
*/ | |
?> | |
<div class="ciptaloka-helloworld-block" style="text-align: <?= $this->getTextAlign(); ?>;"> | |
<p><?= $this->getHelloWorldTxt() . " -- <b>" . $this->getBlockLabel() . "</b>"; ?></p> | |
<ul> | |
<?php foreach ($this->getCollection() as $item) { ?> | |
<li><small><b><?= $item->getLabel(); ?></b>: <?= $item->getValue(); ?></small></li> | |
<?php } ?> | |
</ul> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment