2

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);
    }

    ...
    ...
}
  • Share construct() function of your collection class. – Prince Patel Sep 19 '17 at 7:03
  • @PrincePatel Please check I updated my Question. – Rahul Sep 19 '17 at 7:09
31

$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,
    ...
) {
    ...
}
  • Excellent. This solution works great for Absolute theme 1.1.2 on Magento 2.2.x – Antony Dec 22 '17 at 4:19
2

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.

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.