---
title: "How to Join Two Tables and Create Admin Grid using UI Component in Magento 2?"
url: "https://magecomp.com/blog/join-two-tables-and-create-admin-grid-using-ui-component-magento-2/"
date: "2022-09-23T12:55:19+00:00"
modified: "2025-02-14T07:06:15+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 723
reading_time: "4 min read"
summary: "Hello Magento Friends,"
description: "Check out the step by step guide for How to Join Two Tables and Create Admin Grid using UI Component in Magento 2."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Create SimpleXMLElement Object in Magento 2"
    url: "https://magecomp.com/blog/create-simplexmlelement-object-magento-2/"
  - title: "How to Convert Layout XML Content into Array in Magento 2?"
    url: "https://magecomp.com/blog/convert-layout-xml-content-into-array-magento-2/"
  - title: "A Complete Guide on Keeping Magento 2 Store Maintained/Up to Date"
    url: "https://magecomp.com/blog/complete-guide-on-keeping-magento-2-store-maintained/"
---

# How to Join Two Tables and Create Admin Grid using UI Component in Magento 2?

_Published: September 23, 2022_  
_Author: Dhiren Vasoya_  

![How to Join Two Tables and Create Admin Grid using UI Component in Magento 2](https://magecomp.com/blog/wp-content/uploads/2022/09/How-to-Join-Two-Tables-and-Create-Admin-Grid-using-UI-Component-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

In this tutorial, I will explain How to Join Two Tables and Create Admin Grid using UI Component in Magento 2?
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2023/10/Magento-2-Speed-Performance-Optimization-Services-CTA-4.webp)](https://magecomp.com/magento-performance-optimization.html)

The Admin grid manages, sorts, and filters various data in Magento 2. Admin Grid is used to perform the update and delete actions. You can [**Create UI Component Admin Grid in Magento 2**](https://magecomp.com/blog/create-ui-component-grid-and-form-magento-2/).
If you want to join two tables in Admin Grid using UI Component in Magento 2, you can use the below steps.
## Steps to Join Two Tables and Create Admin Grid using UI Component in Magento 2:
**Step 1:** Firstly, you need to edit the **di.xml** file. For that go to the below path
**app/code/Vendor/Extension/etc/di.xml**

Now add the code as follows
```
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- here we remove virtualType and defile collection as follow-->
    <type name="VendorExtensionModelResourceModelCustomGridCollection">
        <arguments>
            <argument name="mainTable" xsi:type="string">magecomp_customtable</argument>
            <argument name="eventPrefix" xsi:type="string">magecomp_grid_collection</argument>
            <argument name="eventObject" xsi:type="string">magecomp_collection</argument>
            <argument name="resourceModel" xsi:type="string">VendorExtensionModelResourceModelCustom</argument>
        </arguments>
    </type>
    <type name="MagentoFrameworkViewElementUiComponentDataProviderCollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="grid_record_grid_list_data_source" xsi:type="string">VendorExtensionModelResourceModelGridGridCollection</item>
            </argument>
        </arguments>
    </type>
</config>
```

**Step 2:** Next, create the **Collection.php** file at the below file path
**app/code/Vendor/Extension/Model/ResourceModel/Custom/Collection.php**

Then add the below-mentioned code
```
<?php

namespace VendorExtensionModelResourceModelCustom;

use MagentoFrameworkDataCollectionEntityFactoryInterface;
use PsrLogLoggerInterface;
use MagentoFrameworkDataCollectionDbFetchStrategyInterface;
use MagentoFrameworkEventManagerInterface;
use MagentoStoreModelStoreManagerInterface;
use MagentoFrameworkDBAdapterAdapterInterface;
use MagentoFrameworkModelResourceModelDbAbstractDb;

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

    public function __construct(
        EntityFactoryInterface $entityFactory,
        LoggerInterface $logger,
        FetchStrategyInterface $fetchStrategy,
        ManagerInterface $eventManager,
        StoreManagerInterface $storeManager,
        AdapterInterface $connection = null,
        AbstractDb $resource = null
    )
    {
        $this->_init('VendorExtensionModelCustom', 'VendorExtensionModelResourceModelCustom');

        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
        $this->storeManager = $storeManager;
    }

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

        $this->getSelect()->joinLeft(
            ['secondTable' => $this->getTable('second_table_name')],
            'main_table.record_id = secondTable.record_id',
            '*'
        );
        /* '*' define that you want all column of 2nd table. if you want some particular column then you can define as ['column1','column2'] */
    }
}
```

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-5.webp)](https://magecomp.com/services/hyva-theme-development/)

**Step 3:** Then, create **Collection.php** at the below file path
**Vendor\Extension\Model\ResourceModel\Custom\Grid\Collection**

And add the following code snippet
```
<?php

namespace VendorExtensionModelResourceModelCustomGrid;

use MagentoFrameworkApiSearchSearchResultInterface;
use MagentoFrameworkSearchAggregationInterface;
// Your ResourceModel Collection File Path
use VendorExtensionModelResourceModelCustomCollection as GridCollection;
use MagentoFrameworkDataCollectionEntityFactoryInterface;
use PsrLogLoggerInterface;
use MagentoFrameworkDataCollectionDbFetchStrategyInterface;
use MagentoFrameworkEventManagerInterface;
use MagentoStoreModelStoreManagerInterface;
use MagentoFrameworkModelResourceModelDbAbstractDb;

class Collection extends GridCollection implements SearchResultInterface
{
    public function __construct(
        EntityFactoryInterface $entityFactory,
        LoggerInterface $logger,
        FetchStrategyInterface $fetchStrategy,
        ManagerInterface $eventManager,
        StoreManagerInterface $storeManager,
        $mainTable,
        $eventPrefix,
        $eventObject,
        $resourceModel,
        $model = 'MagentoFrameworkViewElementUiComponentDataProviderDocument',
        $connection = null,
        AbstractDb $resource = null
    )
    {
        parent::__construct(
            $entityFactory,
            $logger,
            $fetchStrategy,
            $eventManager,
            $storeManager,
            $connection,
            $resource
        );
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
    }

    public function getAggregations()
    {
        return $this->aggregations;
    }

    public function setAggregations($aggregations)
    {
        $this->aggregations = $aggregations;
    }

    public function getSearchCriteria()
    {
        return null;
    }

    public function setSearchCriteria(
        MagentoFrameworkApiSearchCriteriaInterface $searchCriteria = null
    ) {
        return $this;
    }

    public function getTotalCount()
    {
        return $this->getSize();
    }

    public function setTotalCount($totalCount)
    {
        return $this;
    }

    public function setItems(array $items = null)
    {
        return $this;
    }
}
```

## [![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3.webp)](https://magecomp.com/services/hire-magento-programmer/)

## Conclusion:
Hence, using the above steps, you can quickly join two tables and create an admin grid using the UI component in Magento 2. If you have any doubts, ask me through the comment section. Share the article with your friends and stay connected with us.
**Happy Coding!**

[![Magento-development-services](https://magecomp.com/blog/wp-content/uploads/2020/06/Hire-Magento-4.png)](https://magecomp.com/magento-development-services?utm_source=magecomp&utm_medium=blog&utm_campaign=cta)


---

_View the original post at: [https://magecomp.com/blog/join-two-tables-and-create-admin-grid-using-ui-component-magento-2/](https://magecomp.com/blog/join-two-tables-and-create-admin-grid-using-ui-component-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-11-04 15:12:04 UTC_  
