Question

I have compilation issue on my custom module. Code works properly in magento 2.1 but in magento 2.2 it's shows error at compilation like this

Incompatible argument type: Required type: \Magento\Framework\DB\Adapter\AdapterInterface. Actual type: \Vendor\Module\Model\ResourceModel\Products\type; File: /var/www/html/magento2.2/app/code/Vendor/Module/Model/ResourceModel/Products/Collection.php

<?php

namespace Vendor\Module\Model\ResourceModel\Products;

use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    public function __construct(
    EntityFactoryInterface $entityFactory,
    LoggerInterface $logger,
    FetchStrategyInterface $fetchStrategy,
    ManagerInterface $eventManager,
    $connection = null,
    AbstractDb $resource = null
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
    }

    ...
    ...
}
Was it helpful?

Solution

$connection need to be an instance of \Magento\Framework\DB\Adapter\AdapterInterface, so update your __contruct() like this

public function __construct(
    ...
    \Magento\Framework\DB\Adapter\AdapterInterface  $connection = null,
    ...
) {
    ...
}

OTHER TIPS

You just need to changes in comment code like as

    /**
     * Collection constructor
     *
     * @param Context $context
     * @param string|null $connection
     * @param xyz
     */
public function __construct(
    EntityFactoryInterface $entityFactory,
    LoggerInterface $logger,
    FetchStrategyInterface $fetchStrategy,
    ManagerInterface $eventManager,
    $connection = null,
    AbstractDb $resource = null
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, 
        $eventManager, $connection, $resource);
    }

just set this "@param string|null $connection" comment solved your issue no need to defined class.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top