Skip to main content
added 45 characters in body
Source Link
medina
  • 585
  • 5
  • 11

Using the comments above, I've added the join to the Grid isin a pretty simple and straightforward way. To have the joinjoined tables, you cannot use Virtual type tables. So, that's how you make it happens

In your etc/di.xml add:

<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_module_yourtablemodel_listing_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\YourTableModel\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

Then, create the Collection, Vendor\Module\Model\ResourceModel\YourTableModel\Grid\Collection.php

and add the content below:

<?php

namespace Vendor\Module\Model\ResourceModel\YourTableModel\Grid;

use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Psr\Log\LoggerInterface as Logger;

class Collection extends SearchResult
{
    protected $_idFieldName = 'id';

    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'your_table',
        $resourceModel = 'Vendor\Module\Model\ResourceModel\YourTableModel',
        $identifierName = null,
        $connectionName = null
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel, $identifierName, $connectionName);
    }

    /**
     * @return Collection|void
     */
    protected function _initSelect()
    {
        parent::_initSelect();

        // Join the 2nd Table
        $this->getSelect()
            ->joinLeft(
                ['some_other_table_alias' => $this->getConnection()->getTableName('some_other_table')],
                'main_table.some_other_table_id = some_other_table_alias.id',
                ['some_other_table_column1', 'some_other_table_column2']
            );
    }
}

Flush the cache and it should work.

Using the comments above, I've added the join to the Grid is pretty simple and straightforward way. To have the join, you cannot use Virtual type tables.

In your etc/di.xml add:

<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_module_yourtablemodel_listing_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\YourTableModel\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

Then, create the Collection, Vendor\Module\Model\ResourceModel\YourTableModel\Grid\Collection.php

and add the content below:

<?php

namespace Vendor\Module\Model\ResourceModel\YourTableModel\Grid;

use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Psr\Log\LoggerInterface as Logger;

class Collection extends SearchResult
{
    protected $_idFieldName = 'id';

    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'your_table',
        $resourceModel = 'Vendor\Module\Model\ResourceModel\YourTableModel',
        $identifierName = null,
        $connectionName = null
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel, $identifierName, $connectionName);
    }

    /**
     * @return Collection|void
     */
    protected function _initSelect()
    {
        parent::_initSelect();

        // Join the 2nd Table
        $this->getSelect()
            ->joinLeft(
                ['some_other_table_alias' => $this->getConnection()->getTableName('some_other_table')],
                'main_table.some_other_table_id = some_other_table_alias.id',
                ['some_other_table_column1', 'some_other_table_column2']
            );
    }
}

Flush the cache and it should work.

Using the comments above, I've added the join to the Grid in a pretty simple and straightforward way. To have the joined tables, you cannot use Virtual type tables. So, that's how you make it happens

In your etc/di.xml add:

<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_module_yourtablemodel_listing_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\YourTableModel\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

Then, create the Collection, Vendor\Module\Model\ResourceModel\YourTableModel\Grid\Collection.php

and add the content below:

<?php

namespace Vendor\Module\Model\ResourceModel\YourTableModel\Grid;

use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Psr\Log\LoggerInterface as Logger;

class Collection extends SearchResult
{
    protected $_idFieldName = 'id';

    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'your_table',
        $resourceModel = 'Vendor\Module\Model\ResourceModel\YourTableModel',
        $identifierName = null,
        $connectionName = null
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel, $identifierName, $connectionName);
    }

    /**
     * @return Collection|void
     */
    protected function _initSelect()
    {
        parent::_initSelect();

        // Join the 2nd Table
        $this->getSelect()
            ->joinLeft(
                ['some_other_table_alias' => $this->getConnection()->getTableName('some_other_table')],
                'main_table.some_other_table_id = some_other_table_alias.id',
                ['some_other_table_column1', 'some_other_table_column2']
            );
    }
}

Flush the cache and it should work.

Source Link
medina
  • 585
  • 5
  • 11

Using the comments above, I've added the join to the Grid is pretty simple and straightforward way. To have the join, you cannot use Virtual type tables.

In your etc/di.xml add:

<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_module_yourtablemodel_listing_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\YourTableModel\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

Then, create the Collection, Vendor\Module\Model\ResourceModel\YourTableModel\Grid\Collection.php

and add the content below:

<?php

namespace Vendor\Module\Model\ResourceModel\YourTableModel\Grid;

use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Psr\Log\LoggerInterface as Logger;

class Collection extends SearchResult
{
    protected $_idFieldName = 'id';

    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'your_table',
        $resourceModel = 'Vendor\Module\Model\ResourceModel\YourTableModel',
        $identifierName = null,
        $connectionName = null
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel, $identifierName, $connectionName);
    }

    /**
     * @return Collection|void
     */
    protected function _initSelect()
    {
        parent::_initSelect();

        // Join the 2nd Table
        $this->getSelect()
            ->joinLeft(
                ['some_other_table_alias' => $this->getConnection()->getTableName('some_other_table')],
                'main_table.some_other_table_id = some_other_table_alias.id',
                ['some_other_table_column1', 'some_other_table_column2']
            );
    }
}

Flush the cache and it should work.