0

I face one issue when get collection in admin grid.

Call to undefined method Vendor\Module\Model\ResourceModel\VendorTest\ModuleTest\TestCollection::getMainTable()

Vendor\Module\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="vendor_test_grid_vendor_test_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\VendorTest\ModuleTest\TestCollection</item>
            </argument>
        </arguments>
    </type>
    <type name="Vendor\Module\Storage\Meta">
        <arguments>
            <argument name="dateTime" xsi:type="object">\Magento\Framework\Stdlib\DateTime\DateTime\Proxy</argument>
        </arguments>
    </type>
</config>

Vendor\Module\view\adminhtml\ui_component\vendor_test_grid_vendor_test.xml

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">vendor_test_grid_vendor_test.vendor_test_grid_vendor_test_data_source</item>
            <item name="deps" xsi:type="string">vendor_test_grid_vendor_test.vendor_test_grid_vendor_test_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">columns</item>
    </argument>
    <dataSource name="vendor_test_grid_vendor_test_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
            <argument name="name" xsi:type="string">vendor_test_grid_vendor_test_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">hash</argument>
            <argument name="requestFieldName" xsi:type="string">hash</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">hash</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>
    <listingToolbar name="listing_top">
        <paging name="listing_paging"/>
    </listingToolbar>
    <columns name="columns">
        <column name="catId">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">Category ID</item>
                </item>
            </argument>
        </column>
        <column name="name">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Name</item>
                </item>
            </argument>
        </column>
        <column name="storeId">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Store ID</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>
0

change di.xml:

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="test_index_listing_data_source" xsi:type="string"><vendor>\<module>\Model\ResourceModel\<modeilName>\Grid\Collection</item>
        </argument>
    </arguments>
</type>
<type name="<vendor>\<module>\Model\ResourceModel\<modelName>\Grid\Collection">
    <arguments>
        <argument name="mainTable" xsi:type="string">vendor_module</argument>
        <argument name="eventPrefix" xsi:type="string">test__grid_collection</argument>
        <argument name="eventObject" xsi:type="string">test_grid_collection</argument>
        <argument name="resourceModel" xsi:type="string"><vendor>\<module>\Model\ResourceModel\<modelName></argument>
    </arguments>
</type>
5
  • Can I get collection without mention mainTable name? Oct 22 '20 at 12:16
  • i don't think so, How would magento know about table if you don't mention name. Oct 22 '20 at 12:21
  • I think, it is possible. Oct 22 '20 at 12:46
  • If you got solution then please post it here. else you can apply my solution and accept answer. that would help. Oct 22 '20 at 14:27
  • Yes, sure. I will update here when got a proper solution for that. Oct 23 '20 at 5:39

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.