1

I have referred this link

Reference link

Now where I have to place a code for join query multiple table for custom admin grid in magento 2

I have referred many links but got failed for ex.

adding below code in

Company\Module\Model\ResourceModel\Credits\collection.php

    protected function _initSelect()
        {
            echo 'hererer';
            die;
            parent::_initSelect();

            $this->getSelect()->joinLeft(
                    ['secondTable' => $this->getTable('customer_grid_flat')],
                    'main_table.customer_id = secondTable.entity_id',
                    array('*')
                );
            $this->addFilterToMap('firstname', 'secondTable.name');
            //$this->addFilterToMap('columnname2_clias', 'secondTable.columnname2');
            return $this;
        }


    <columns name="credit_listing_columns">
            <column name="customer_id">
                <settings>
                    <filter>text</filter>
                    <label translate="true">Customer ID</label>
                    <sorting>asc</sorting>
                </settings>
            </column>
            <column name="firstname">
                <settings>
                    <filter>text</filter>
                    <label translate="true">Customer Name</label>
                </settings>
            </column>
            <column name="credit">
...

what is the proper magento procedure or what is wrong with this, also I tried to echo die; inside this function but got failed.

SagarPPanchal
  • 352
  • 1
  • 3
  • 17

1 Answers1

1

You can add the join query in ui component data source.
Code in module's di.xml

----
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
            <arguments>
                <argument name="collections" xsi:type="array">
                    <item name="anshu_custom_listing_data_source" xsi:type="string">Anshu\Custom\Model\ResourceModel\ActiveSessions\Grid\Collection</item>
                </argument>
            </arguments>
        </type>
        <virtualType name="Anshu\Custom\Model\ResourceModel\ActiveSessions\Grid\Collection">
            <arguments>
                <argument name="mainTable" xsi:type="string">anshu_custom_table</argument>
                <argument name="resourceModel" xsi:type="string">Anshu\Custom\Model\ResourceModel\ActiveSessions</argument>
            </arguments>
        </virtualType>
----

Code in app/code/Anshu/Custom/Model/ResourceModel/ActiveSessions/Grid/Collection.php

----
protected function _initSelect()
    {
        parent::_initSelect();

        $this->getSelect()->joinLeft(-----);

        return $this;
    }
----

Code in ui component xml file

-----
<dataProvider class="Anshu\Custom\Ui\Component\DataProvider" name="anshu_custom_listing_data_source">
----

Anshu\Custom\Model\ResourceModel\ActiveSessions\Grid\Collection is my ui component grid data source provider.

Anshu Mishra
  • 8,950
  • 7
  • 40
  • 88