0

I have the fallowing collection and I want to pass the ProductFactory so it can be accessible in my collection.

class Collection extends AbstractCollection
    {
        public $productFactory;

        /**
         * @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,
            ProductFactory $productFactory
        ) {
            parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
            $this->productFactory = $productFactory->create();
        }

        protected function _construct()
        {
            $this->_init(MyClass::class,MyResource::class);
        }

The error that I get is

Uncaught Error: Cannot instantiate interface Magento\Framework\DB\Adapter\AdapterInterface in  

/var/www/html/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php on line 73

What am I doing wrong, is the goal that I am trying to achieve possible ?

3

For some reason I had to remove

\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,

from the __construct() completely. Removing

= null

for both still didn't do the trick, removing them complete made everything work again.

The new __construct() looks like that:

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,
        ProductFactory $productFactory
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager);
        $this->productFactory = $productFactory->create();
    }

Solved.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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