I have created magento 2 module with single table that and trying to create admin grid with that module but i am getting below error
Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught TypeError: Argument 5 passed to Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::__construct() must implement interface Magento\Framework\DB\Adapter\AdapterInterface, instance of Magento\Store\Model\StoreManager given, called in D:\wamp64\www\magento2\app\code\Vendor\Module\Model\ResourceModel\Modelname\Grid\Collection.php on line 55 in D:\wamp64\www\magento2\vendor\magento\module-ui\Component\Wrapper\UiComponent.php on line 0
Here is my collection code
<?php
namespace Vendor\Module\Model\ResourceModel\Sample\Grid;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Api\Search\AggregationInterface;
use Vendor\Module\Model\ResourceModel\Sample\Collection as SampleCollection;
class Collection extends SampleCollection implements SearchResultInterface
{
/**
* @var AggregationInterface
*/
protected $aggregations;
/**
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param mixed|null $mainTable
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $eventPrefix
* @param mixed $eventObject
* @param mixed $resourceModel
* @param string $model
* @param null $connection
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
$mainTable,
$eventPrefix,
$eventObject,
$resourceModel,
$model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
$connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$storeManager,
$metadataPool,
$connection,
$resource
);
$this->_eventPrefix = $eventPrefix;
$this->_eventObject = $eventObject;
$this->_init($model, $resourceModel);
$this->setMainTable($mainTable);
}
/**
* @return AggregationInterface
*/
public function getAggregations()
{
return $this->aggregations;
}
/**
* @param AggregationInterface $aggregations
* @return $this
*/
public function setAggregations($aggregations)
{
$this->aggregations = $aggregations;
}
/**
* Get search criteria.
*
* @return \Magento\Framework\Api\SearchCriteriaInterface|null
*/
public function getSearchCriteria()
{
return null;
}
/**
* Set search criteria.
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
{
return $this;
}
/**
* Get total count.
*
* @return int
*/
public function getTotalCount()
{
return $this->getSize();
}
/**
* Set total count.
*
* @param int $totalCount
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setTotalCount($totalCount)
{
return $this;
}
/**
* Set items list.
*
* @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setItems(array $items = null)
{
return $this;
}
}
Vendor\Module\Model\ResourceModel\Sample\Collection.php
<?php
namespace Vendor\Module\Model\ResourceModel\Sample;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected function _construct()
{
$this->_init('Vendor\Module\Model\Sample','Vendor\Module\Model\ResourceModel\Sample');
}
}
1 Answer 1
The problem is the order of the parameters in your parent::__construct() call.
the class your are extending does not have a constructor so it the parent of the parent constructor will be called.
And this parent of parent is \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection.
It's constructor looks like this:
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
$this->_eventManager = $eventManager;
parent::__construct($entityFactory, $logger, $fetchStrategy, $connection);
$this->_construct();
$this->_resource = $resource;
$this->setConnection($this->getResource()->getConnection());
$this->_initSelect();
}
so the 5th parameter must be something that implements \Magento\Framework\DB\Adapter\AdapterInterface.
In your case, the 5th parameter is something that implementes \Magento\Store\Model\StoreManagerInterface.
Change your parent::__construct() call to this:
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$connection,
$resource
);
the parameters $storeManager and $metadataPool are not part of the classes you extend. If you need them in your own class you can add them in the __construct method like this:
$this->storeManager = $storeManager;
$this->metadataPool = $metadataPool;
and declare them as protected members as you did for $aggregations.
-
Hi @Marius, If I extends \Magento\Eav\Model\Entity\Collection\AbstractCollection then, what should I need to change in construct ?Dharmik Ponda– Dharmik Ponda2019年02月25日 11:00:31 +00:00Commented Feb 25, 2019 at 11:00
-
Current, I get error Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught TypeError: Argument 5 passed to Magento\Eav\Model\Entity\Collection\AbstractCollection::__construct() must be an instance of Magento\Eav\Model\Config, null given, called in /var/www/html/mg226/app/code/VendorName/ModuleName/Model/ResourceModel/Mainpage/Grid/Collection.php on line 80 in /var/www/html/mg226/vendor/magento/module-ui/Component/Wrapper/UiComponent.php on line 0Dharmik Ponda– Dharmik Ponda2019年02月25日 11:01:16 +00:00Commented Feb 25, 2019 at 11:01
-
Perfect. It's working.Dhaduk Mitesh– Dhaduk Mitesh2019年12月20日 05:55:42 +00:00Commented Dec 20, 2019 at 5:55