0
public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entity,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $event,
        \Magento\Store\Model\StoreManagerInterface $store,
        \Magento\Customer\Model\Session $customerSession,
        $mainTable,
        $eventPrefix,
        $eventObject,
        $resourceModel,
        $model = \Magento\Framework\View\Element\UiComponent\DataProvider\Document::class,
        $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {
        parent::__construct($entity, $logger, $fetchStrategy, $event, $store, $connection, $resource);
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
        $this->_customerSession = $customerSession;
    }

I want to change the parameter of the constructor. This module is installed by the composer and while compiling the code I am getting the issue

Incompatible argument type: Required type: \Magento\Framework\DB\Adapter\AdapterInterface. Actual type: \Magento\Framework\Model\ResourceModel\Db\AbstractDb; File:

So I am adding another module in the app/code/ directory and passing these argument to solve the issue

public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entity,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $event,
        \Magento\Store\Model\StoreManagerInterface $store,
        \Magento\Customer\Model\Session $customerSession,
        $mainTable,
        $eventPrefix,
        $eventObject,
        $resourceModel,
        $model = \Magento\Framework\View\Element\UiComponent\DataProvider\Document::class,
        \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {
        parent::__construct($entity, $logger, $fetchStrategy, $event, $store, $connection, $resource);
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
        $this->_customerSession = $customerSession;
    }

I am using preference to override the same file. Actually When I am passing the dependency in the core module then it is working fine but I don't want to make any changes in this module. then How can I replace these arguments using a custom module.

2 Answers 2

0

You can change the parameters by dependency injection

However, this is a rather difficult way that requires you to know the basics of Magento.

You can refer to this document to better understand the method I have given you: https://mage2.pro/t/topic/215

0

You can do this in your di.xml file.

I don't know which class you are trying to change the constructor for, but you'd take that class, say Magento\Some\Class, and use that as the type below.

You'll then define the argument names and values that you'd like to pass in. It appears that the last three arguments are what you would like to change.

Rather than using a class preference, it's a much better idea to use di.xml here with argument substitution:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Some\Class">
        <arguments>
            <argument name="model" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\Document::class</argument>
            <argument name="connection" xsi:type="object">Magento\Framework\DB\Adapter\AdapterInterface</argument>
            <argument name="resource" xsi:type="object">Magento\Framework\Model\ResourceModel\Db\AbstractDb</argument>
        </arguments>
    </type>
</config>

Your Answer

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

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