Created
November 27, 2024 00:38
-
-
Save markshust/5faca0f2ac9e350c5cb2e95e00443638 to your computer and use it in GitHub Desktop.
Fetch data from multiple tables in a Magento admin grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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