5

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

    ...
    ...
}
0

2 Answers 2

37

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

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.

1
  • 1
    This was the solution that worked for me; I had the error Incompatible argument type: Required type: array. Actual type: \Vendor\Module\Same\As\Namespace\data (notice the "actual type" matched the declared namespace), and the fix was to change @param [] $data to @param array $data in the constructor block comment, and it worked a charm! It's very strange how incorrect comments can stop the entire site from compiling.
    – Smithee
    Feb 11, 2020 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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