0

I have created a custom module and it's working fine,if I only run php bin/magento setup:upgrade command. However, when I attempt to run php bin/magento s:d:c it raises an exception.

Type Error occurred when creating object: Vendor\ModuleName\Model\ResourceModel\PriceResult\Collection\Interceptor, Argument 5 passed to Vendor\ModuleName\Model\ResourceModel\PriceResult\Collection\Interceptor::__construct() must implement interface Magento\Framework\DB\Adapter\AdapterInterface or be null, string given, called in /Users/macbook/Sites/magento243/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 121
Exception in /Users/macbook/Sites/magento243/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:131

Here is my collection class

<?php

namespace Vendor\ModuleName\Model\ResourceModel\PriceResult;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected $_idFieldName = 'id';

    protected function _construct()
    {
        $this->_init(
            \Vendor\ModuleName\Model\PriceResult::class,
            \Vendor\ModuleName\Model\ResourceModel\PriceResult::class,
        );
    }
}

etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <virtualType name="Vendor\ModuleName\Model\ResourceModel\SyncPriceResult\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">price_result</argument>
            <argument name="resourceModel" xsi:type="string">Vendor\ModuleName\Model\ResourceModel\SyncPriceResult</argument>
        </arguments>
    </virtualType>
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="price_result_listing_data_source" xsi:type="string">Vendor\ModuleName\Model\ResourceModel\SyncPriceResult\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

1 Answer 1

0

To overcome this issue, I made the adjustment by updating the virtual type namespace as follows: VendorName\ModuleName\Model\ResourceModel\Grid\Grid\Collection

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    
    <virtualType name="VendorName\ModuleName\Model\ResourceModel\Grid\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">price_result</argument>
            <argument name="resourceModel" xsi:type="string">VendorName\ModuleName\Model\ResourceModel\Grid</argument>
        </arguments>
    </virtualType>
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="price_result_listing_data_source" xsi:type="string">VendorName\ModuleName\Model\ResourceModel\Grid\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

This resource was also instrumental in helping me identify and address the problem: Type Error occurred when creating object Magento 2

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.