Skip to content

Instantly share code, notes, and snippets.

@markshust
Created November 27, 2024 00:38
Show Gist options
  • Save markshust/5faca0f2ac9e350c5cb2e95e00443638 to your computer and use it in GitHub Desktop.
Save markshust/5faca0f2ac9e350c5cb2e95e00443638 to your computer and use it in GitHub Desktop.
Fetch data from multiple tables in a Magento admin grid
<?php
namespace Macademy\Minerva\Model\ResourceModel\Faq\Grid;
class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
protected function _initSelect()
{
parent::_initSelect();
// Add your join statements here
$this->getSelect()->joinLeft(
['second_table' => $this->getTable('your_second_table')],
'main_table.foreign_key = second_table.id',
['second_table_column1', 'second_table_column2']
);
// You can add more joins if needed
$this->getSelect()->joinLeft(
['third_table' => $this->getTable('your_third_table')],
'main_table.another_foreign_key = third_table.id',
['third_table_column1']
);
return $this;
}
}
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Macademy\Minerva\Model\ResourceModel\Faq\Grid\Collection" type="Macademy\Minerva\Model\ResourceModel\Faq\Grid\Collection">
<arguments>
<argument name="mainTable" xsi:type="string">macademy_minerva_faq</argument>
<argument name="resourceModel" xsi:type="string">Macademy\Minerva\Model\ResourceModel\Faq</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="macademy_minerva_faq_listing_data_source" xsi:type="string">Macademy\Minerva\Model\ResourceModel\Faq\Grid\Collection</item>
</argument>
</arguments>
</type>
</config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment