2

I have a CRUD module created on magento 2.1.6 version which was working ok untill i updated magento to version 2.2.1

Now when i compile my code i receive the following err:

namespace\module\Model\ResourceModel\Post\Grid\Collection
Incompatible argument type: Required type: \Magento\Framework\DB\Adapter\AdapterInterface. Actual type: \namespace\module\Model\ResourceModel\Post\Grid\connection; File:
/data/web/public/app/code/namespace/module/Model/ResourceModel/Post/Grid/Collection.php

so my constructor looks like this before i made any changes:

 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,
        $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, $connection, $resource);
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
    }

after i changed the constructor according to the compile error my 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,
        $mainTable,
        $eventPrefix,
        $eventObject,
        $resourceModel,
        $model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',

        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    )
    {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
    }

Now when i compile the code i don't have any err, but when i tried to access my "module menu" i receive this err:

PHP message: PHP Fatal error:  Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught Error: Cannot instantiate interface Magento\Framework\DB\Adapter\AdapterInterface

IT said that "Magento\Framework\DB\Adapter\AdapterInterface" can't be instantiate, even the compile was asking for this.

Does anyone have any idea how to solve this problem?

1 Answer 1

0

You need to use \Magento\Framework\DB\Adapter\AdapterInterface $connection = null instead of $connection = null

Now your final constructor look like below,

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,
    $mainTable,
    $eventPrefix,
    $eventObject,
    $resourceModel,
    $model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
    \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
)
{
    parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
    $this->_eventPrefix = $eventPrefix;
    $this->_eventObject = $eventObject;
    $this->_init($model, $resourceModel);
    $this->setMainTable($mainTable);
}

Now you can use php bin/magento setup:di:compile your error will gone.

4
  • is working like charm. thank you. can you explain me a little why the order of the constructor parameters is so important? Commented Nov 29, 2017 at 11:27
  • We dont have change the order of constructor just add full path for connection object. Commented Nov 29, 2017 at 11:30
  • but i did the same thing and was not working. please check my second constructor Commented Nov 29, 2017 at 11:35
  • in your second constructor you were missing $connection object Commented Nov 29, 2017 at 11: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.