2

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
    Commented Jan 15, 2021 at 9:53
  • @dudzio Yes, compiled and working in dev. But not working in production.
    – Bojjaiah
    Commented Jan 15, 2021 at 13:24
  • In your di.xml try changing the xsi:type to object for the resourceModel argument
    – Sam
    Commented Jan 20, 2021 at 17:00
  • 1
    @Sam you shoud submit that as the answer. you are correct Commented Jan 20, 2021 at 22:37

8 Answers 8

12

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.

3
  • 2
    Thank you for the explanation. This fixed my issue. However besides flushing the cache you should also recompile bin/magento setup:di:compile.
    – Zankar
    Commented Nov 3, 2021 at 13:06
  • This fixed my issue. Thanks @Lokesh Commented Feb 10, 2023 at 19:58
  • Fixed issue, Still want to understand what confliction is happening at the time. Commented Apr 30, 2023 at 5:02
3
+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.

3
  • Thank you, let me try
    – Bojjaiah
    Commented Jan 21, 2021 at 4:28
  • this code is correct? or where to change xsi:type to object?? Commented Feb 16, 2022 at 8:57
  • @BhaveshBhuva on the resourceModel argument tag
    – Sam
    Commented Feb 17, 2022 at 9:06
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

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

On the di.xml

Check if you have the path like these "Vendor\Blog\Model\ResourceModel\Post\Grid\Collection" added to your item tag value.

 <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="vendor_blog_admin_ui_post_listing_data_source" xsi:type="string">Vendor\Blog\Model\ResourceModel\Post\Grid\Collection</item>
        </argument>
    </arguments>
</type>

If so you will want to change it to something like this:

"VendorBlogCollection"

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="vendor_blog_admin_ui_post_listing_data_source" xsi:type="string">VendorBlogCollection</item>
        </argument>
    </arguments>
</type>

And then change the virtualType name to "VendorBlogCollection" as well.

<virtualType name="VendorBlogCollection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
    <arguments>
        <argument name="mainTable" xsi:type="string">vendor_blog_post</argument>
        <argument name="resourceModel" xsi:type="string">Vendor\Blog\Model\ResourceModel\Post</argument>
    </arguments>
</virtualType>

Run php bin/magento se:up

Hope it works :)

-1

php bin/magento s:up

php bin/magento s:s:d -f

php bin/magento c:f

-1

Try it

php bin/magento setup:di:compile

php bin/magento c:c

php bin/magento c:f
-1

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

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.