0

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?

4
  • Have you run bin/magento setup:upgrade?
    – dudzio
    Jan 15 at 9:53
  • @dudzio Yes, compiled and working in dev. But not working in production.
    – Bojjaiah
    Jan 15 at 13:24
  • In your di.xml try changing the xsi:type to object for the resourceModel argument
    – Sam
    Jan 20 at 17:00
  • 1
    @Sam you shoud submit that as the answer. you are correct Jan 20 at 22:37
2
+50

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.

1
  • Thank you, let me try
    – Bojjaiah
    Jan 21 at 4:28
0

try it:-

<?php

namespace Tiny\ProductCustomization\Model\ResourceModel\Product;


class Collection extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

    protected $_idFieldName = 'id';

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

}

follow step:-

php bin/magento s:up

php bin/magento s:s:d -f

php bin/magento c:f

0

php bin/magento s:up

php bin/magento s:s:d -f

php bin/magento c:f

0

Try it

php bin/magento setup:di:compile

php bin/magento c:c

php bin/magento c:f
0

It sounds like your collection instance is receiving a string instead of a class as argument for resource model (5th argument).

Usually, magento uses ::class to initialize model & resource model with __init() method call, you can try the code below to check if it solves your error with string argument vs AdapterInterface implementation argument :

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

    parent::_construct();
}

Hope it will help !

0

In mode production you must recompile to regenerate the new interceptor.

bin/magento setup:di:compile

If not work share please the content of the file:

Tiny\ProductCustomization\Model\ResourceModel\Product\Collection\Interceptor

Thanks

0

You are creating a virtual type with the namespace Tiny\ProductCustomization\Model\ResourceModel\Product\Collection which already exists.

As a result, AbstractFactory will pass the arguments (mainTable and resourceModel) to the __construct function of AbstractCollection instead of SearchResult, which throws an exception because AbstractCollection does not accept the arguments as a string whereas SearchResult does.

To solve the problem, you have to rename the virtual type namespace to Tiny\ProductCustomization\Model\ResourceModel\Product\Grid\Collection.

See the code below:

<virtualType name="Tiny\ProductCustomization\Model\ResourceModel\Product\Grid\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>
<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\Grid\Collection</item>
        </argument>
    </arguments>
</type>

Flush cache with php bin/magento c:f and check again.

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.