2

I'm trying to override a Collection constructor and getting

Fatal error: Uncaught Error: Cannot instantiate interface Magento\Framework\DB\Adapter\AdapterInterface in /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93

As well as

Error: Cannot instantiate interface Magento\Framework\DB\Adapter\AdapterInterface in /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93

I have tried:

  1. Look in the core for any preference for Magento\Framework\DB\Adapter\AdapterInterface with no luck.

  2. setup:di:compile with no luck.

The problem was also raised here: How dependency injection works for Interfaces in constructors with no answer though.

Any help will much appreciated.

EDIT

So the problem was not overriding the Collection constructor.

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection {

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,
 $someParam = null
)
{
 parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
 //$this->relationResourceModel = $resourceModel;
}

This will work. The problem was that I was trying to inject a foreign resource model in the collection constructor

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection { protected $relationResourceModel;

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,
 \Vendor\Module\Model\ResourceModel\ResourceModel $resourceModel
)
{
 parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
 $this->relationResourceModel = $resourceModel;
}

This will not work. It would be nice to know the inner workings of this and why this does not work. But I chosen another approach.

asked Sep 6, 2017 at 6:38
1
  • 1
    can you provide code what you have tried so far Commented Sep 6, 2017 at 6:45

2 Answers 2

3

Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.

http://php.net/manual/en/functions.arguments.php

Moreover, in this case connection with type \Magento\Framework\DB\Adapter\AdapterInterface is non-injectable object.

Newable/non-injectable: Objects obtained by creating a new class instance every time. Transient objects, such as those that require external input from the user or database, fall into this category. Attempts to inject these objects produce either an error that the object could not be created or an incorrect object that is incomplete.

http://devdocs.magento.com/guides/v2.2/extension-dev-guide/depend-inj.html

Try to move required arguments before optional

answered Sep 6, 2017 at 10:33
5

You can also just remove \Magento\Framework\DB\Adapter\AdapterInterface from __constructor and for parent::__construct just write null

answered Mar 1, 2018 at 12:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.