I have created Magento 2 Test Module which is connected to DB, it is throwing error while fetching collection. Any Idea on this.
Fatal error: Call to undefined method Learning\Test\Model\Resource\Test\Collection::create() in C:\xampp\htdocs\magento-2\app\code\Learning\Test\Block\Adminhtml\Test\Grid.php on line 40
here i'm giving Model and Block(Container) Code.
my code is:
app\code\Magento\Learning\Test\Model\Test.php
<?php
namespace Learning\Test\Model;
class Test extends \Magento\Framework\Model\AbstractModel
{
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Learning\Test\Model\Resource\Test $resource,
\Learning\Test\Model\Resource\Test\Collection $resourceCollection
) {
parent::__construct(
$context,
$registry,
$resource,
$resourceCollection
);
}
}
?>
app\code\Learning\Test\Model\Resource\Test.php
<?php
namespace Learning\Test\Model\Resource;
class Test extends \Magento\Framework\Model\Resource\Db\AbstractDb
{
protected function _construct()
{
$this->_init('test', 'id');
}
}
?>
app\code\Learning\Test\Model\Resource\Test\Collection.php
<?php
namespace Learning\Test\Model\Resource\Test;
class Collection extends \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection
{
protected function _construct()
{
$this->_init('Learning\Test\Model\Test', 'Learning\Test\Model\Resource\Test');
}
}
?>
app\code\Learning\Test\Controller\Adminhtml\Index\Index.php
<?php
namespace Learning\Test\Controller\Adminhtml\Test;
class Index extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
public function execute()
{
//$this->_title->add(__('Manage Banners'));
if ($this->getRequest()->getQuery('ajax')) {
$this->_forward('grid');
return;
}
$this->_view->loadLayout();
/**
* Set active menu item
*/
$this->_setActiveMenu('test');
/**
* Add breadcrumb item
*/
$this->_addBreadcrumb(__('Records'), __('Records'));
$this->_addBreadcrumb(__('Manage Records'), __('Manage Records'));
$this->_view->renderLayout();
}
}
app\code\Learning\Test\Block\Adminhtml\Test\Test.php
<?php
namespace Learning\Test\Block\Adminhtml;
use \Magento\Backend\Block\Widget\Grid\Container;
class Test extends Container
{
/**
* Constructor
*
* @return void
*/
protected function _construct()
{
$this->_controller = 'adminhtml_test';
$this->_blockGroup = 'Learning_Test';
$this->_headerText = __('Custom Module');
$this->_addButtonLabel = __('Add New Record');
parent::_construct();
}
}
app\code\Learning\Test\Block\Adminhtml\Test\Grid.php
<?php
namespace Learning\Test\Block\Adminhtml\Test;
class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
protected $_bannerFactory;
protected $_banner;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Backend\Helper\Data $backendHelper,
\Learning\Test\Model\Resource\Test\Collection $bannerFactory,
\Learning\Test\Model\Test $banner,
array $data = array()
) {
$this->_bannerFactory = $bannerFactory;
$this->_banner = $banner;
parent::__construct($context, $backendHelper, $data);
}
protected function _construct()
{
parent::_construct();
$this->setId('testGrid');
$this->setDefaultSort('id');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
}
protected function _prepareCollection()
{
//$collection = $this->_bannerFactory->getCollection();
$collection = $this->_bannerFactory->create()->getCollection();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/Collection.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info($collection);
/*
* $collection = array();
*
$adapter = $this->_banner->_getReadAdapter();
$select = $adapter->select()->from($this->_banner->getTable('test'), '*');
$allRows = $adapter->fetchAll($select);
foreach($allRows as $key => $value){
$data = new \Magento\Framework\Object(array('key' => $key, 'value' => $value));
$collection->addItem($data);
}
*/
$this->setCollection($collection);
return parent::_prepareCollection();
}
/**
* @return $this
*/
protected function _prepareColumns()
{
$this->addColumn(
'id',
array(
'header' => __('ID'),
'type' => 'number',
'index' => 'id'
/* 'header_css_class' => 'col-id',
'column_css_class' => 'col-id'*/
)
);
$this->addColumn(
'firstname',
array(
'header' => __('First Name'),
'index' => 'firstname',
'class' => 'xxx'
)
);
$this->addColumn(
'lastname',
array(
'header' => __('Last Name'),
'index' => 'lastname',
'class' => 'xxx'
)
);
$this->addColumn(
'edit',
array(
'header' => __('Edit'),
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => __('Edit'),
'url' => array(
'base' => '*/*/edit',
'params' => array('store' => $this->getRequest()->getParam('store'))
),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores'
/*'header_css_class' => 'col-action',
'column_css_class' => 'col-action'*/
)
);
$block = $this->getLayout()->getBlock('grid.bottom.links');
if ($block) {
$this->setChild('grid.bottom.links', $block);
}
return parent::_prepareColumns();
}
/**
* @return $this
*/
protected function _prepareMassaction()
{
$this->setMassactionIdField('entity_id');
$this->getMassactionBlock()->setFormFieldName('test');
$this->getMassactionBlock()->addItem(
'delete',
array(
'label' => __('Delete'),
'url' => $this->getUrl('testadmin/*/massDelete'),
'confirm' => __('Are you sure?')
)
);
return $this;
}
/**
* @return string
*/
public function getGridUrl()
{
return $this->getUrl('test/*/grid', array('_current' => true));
}
public function getRowUrl($row)
{
return $this->getUrl(
'test/*/edit',
array('store' => $this->getRequest()->getParam('store'), 'id' => $row->getId())
);
}
}
Fatal error: Call to undefined method Learning\Test\Model\Resource\Test\Collection::create() in C:\xampp\htdocs\magento-2\app\code\Learning\Test\Block\Adminhtml\Test\Grid.php on line 40
Any help on this?