Соединить 2 таблицы
В вашем Vendor\Module\Model\ResourceModel\ModelName\Grid\Collection
, добавьте функцию _initSelect (), как показано ниже
protected function _initSelect()
{
parent::_initSelect();
$this->getSelect()->joinLeft(
['secondTable' => $this->getTable('admin_user')], //2nd table name by which you want to join
'main_table.user_id= secondTable.user_id', // common column which available in both table
'*' // '*' define that you want all column of 2nd table. if you want some particular column then you can define as ['column1','column2']
);
}
Присоединяйтесь к 3 столам и более
Используйте thirdTable , четвертую таблицу , чтобы объединить больше таблиц, как показано ниже:
protected function _initSelect()
{
parent::_initSelect();
$this->getSelect()->joinLeft(
['secondTable' => $this->getTable('admin_user')],
'main_table.user_id = secondTable.user_id',
['username']
)->joinLeft(
['thirdTable' => $this->getTable('catalog_product_entity')],
'main_table.product_id = thirdTable.entity_id',
['sku']
);//use fourthTable, fifthTable to join more tables
}
пример
Файл app/code/SAdmin/Cart/etc/di.xml
.
Имя элемента - это имя источника данных, используемого в ваших компонентах пользовательского интерфейса.
<?xml version="1.0"?>
<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="sadmin_cart_index_index_listing_data_source" xsi:type="string">SAdmin\Cart\Model\ResourceModel\Quote\Grid\Collection</item>
</argument>
</arguments>
</type>
</config>
файл app/code/SAdmin/Cart/Model/ResourceModel/Quote/Grid/Collection.php
Обратите внимание, расширяет Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
и устанавливает параметры $mainTable
и $resourceModel
.
namespace SAdmin\Cart\Model\ResourceModel\Quote\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
{
public function __construct(
EntityFactory $entityFactory, Logger $logger, FetchStrategy $fetchStrategy, EventManager $eventManager,
$mainTable = 'quote',
$resourceModel = 'Magento\Quote\Model\ResourceModel\Quote',
$identifierName = null, $connectionName = null
)
{
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel, $identifierName, $connectionName);
}
public function _initSelect()
{
parent::_initSelect();
return $this->getSelect()->joinLeft(
['secondTable' => $this->getTable('customer_group')], //2nd table name by which you want to join
'main_table.customer_group_id= secondTable.customer_group_id', // common column which available in both table
['customer_group_code']// '*' define that you want all column of 2nd table. if you want some particular column then you can define as ['column1','column2']
);
}
}