Magento – Type Error occurred when creating object Magento 2

magento2.3

I have created custom module and it's working fine in Dev, but it's not working on Production. It's throwing Type error.

Type Error occurred when creating object:
Tiny\ProductCustomization\Model\ResourceModel\Product\Collection\Interceptor,
Argument 5 passed to
Tiny\ProductCustomization\Model\ResourceModel\Product\Collection\Interceptor::__construct()
must implement interface Magento\Framework\DB\Adapter\AdapterInterface
or be null, string given, called in
/var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php
on line 121 [] [] [2021-01-11 10:11:47] report.CRITICAL: Type Error
occurred when creating object:
Tiny\ProductCustomization\Model\ResourceModel\Product\Collection\Interceptor
[] []

My collection file as below

<?php

namespace Tiny\ProductCustomization\Model\ResourceModel\Product;

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

class Collection extends AbstractCollection
{

    protected $_idFieldName = 'id';

    protected function _construct()
    {
        $this->_init(
            'Tiny\ProductCustomization\Model\Product',
            'Tiny\ProductCustomization\Model\ResourceModel\Product'
        );
        parent::_construct();
    }

}

and di.xml

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="tiny_grid_data_source" xsi:type="string">Tiny\ProductCustomization\Model\ResourceModel\Product\Collection</item>
            </argument>
        </arguments>
    </type>
    <virtualType name="Tiny\ProductCustomization\Model\ResourceModel\Product\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">tiny_pro</argument>
            <argument name="resourceModel" xsi:type="string">Tiny\ProductCustomization\Model\ResourceModel\Product</argument>
        </arguments>
    </virtualType>

Can you please help me on this?

Best Answer

Within your di.xml for the resourceModel argument set the xsi:type to object. This will resolve the FQN to its relevant class.

<virtualType name="Tiny\ProductCustomization\Model\ResourceModel\Product\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
    <arguments>
        <argument name="mainTable" xsi:type="string">tiny_pro</argument>
        <argument name="resourceModel" xsi:type="string">Tiny\ProductCustomization\Model\ResourceModel\Product</argument>
    </arguments>
</virtualType>

If I'm not mistaken, the reason for it only happening when in production mode and not development mode. When in development mode Interceptors are generated on the fly, so if you never loaded your custom collection while testing you wouldn't see the error. Whereas in production mode it generates the Interceptors during the compilation step, so you would get the error before you can even deploy/access the site.

Related Topic