0

I am creating a custom module in magento 2.4 for admin grid. Tried everything but on grid page getting below error -

    Type Error occurred when creating object:
 Vendor\Module\Model\ResourceModel\Grid\Collection\Interceptor, Argument 5 passed to 
Vendor\Module\Model\ResourceModel\Grid\Collection\Interceptor::__construct() must implement interface 
Magento\Framework\DB\Adapter\AdapterInterface or be null, string given, called in 
/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 121

My Collection.php is as below -

<?php
namespace Vendor\Module\Model\ResourceModel\Grid;
 
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    


    protected function _construct() {
        $this->_init('Vendor\Module\Model\Grid', 'Vendor\Module\Model\ResourceModel\Grid');
    }

    /**
     * @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\Framework\DB\Adapter\AdapterInterface $connection
     * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
     */
    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
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection);
        
    }
}

What should I change in Collection.php?

4
  • Try to pass null as the 5th argument.
    – ZealousWeb
    Commented Mar 25, 2021 at 5:20
  • Can you please post, How? Commented Mar 25, 2021 at 10:14
  • first thing do you really need __counstruct function in that class?
    – ZealousWeb
    Commented Mar 26, 2021 at 11:10
  • Without __construct function also getting same error. Commented Mar 26, 2021 at 11:28

1 Answer 1

0

Try below code

<?php
namespace Vendor\Module\Model\ResourceModel\Grid;
 
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    


    protected function _construct() {
        $this->_init('Vendor\Module\Model\Grid', 'Vendor\Module\Model\ResourceModel\Grid');
    }

    /**
     * @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\DB\Adapter\AdapterInterface|null $connection
     * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
     */
    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
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
    }
}
3
  • Tried above code, and run compile and deploy command as well, still same error. Commented Mar 25, 2021 at 7:39
  • Remove all the files from generated folder using rm -rf generated/*; and run compile and deploy command and then check again. Commented Mar 25, 2021 at 8:16
  • Tried this as well but same error. Commented Mar 25, 2021 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.