I am rendering customer grid in the admin panel with some additional fields from the custom table.
The grid is throwing with duplicate column error.
I have used the below code.
Vendor/Module/etc/adminhtml/di.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCustomerModelResourceModelGridCollection">
<plugin name="sales-rep-customers"
type="VendorModulePluginCustomerModelResourceModelGridCollection"
sortOrder="90"/>
</type>
Vendor/Module/PluginCustomerModelResourceModelGridCollection.php
<?php
namespace VendorModulePluginCustomerModelResourceModelGrid;
use MagentoCustomerModelResourceModelGridCollection as Subject;
use MagentoBackendModelAuthSession;
use MagentoFrameworkAppResourceConnection;
class Collection
{
/**
* @var Session
*/
private $adminSession;
public function __construct(
Session $adminSession,
ResourceConnection $resourceConnection
)
{
$this->adminSession = $adminSession;
$this->connection = $resourceConnection->getConnection();
}
public function aroundLoadWithFilter(
Subject $subject,
callable $proceed,
$printQuery = false,
$logQuery = false
)
{
if (!$this->adminSession->isLoggedIn()) {
return $proceed($printQuery, $logQuery);
}
$select = $this->connection->select();
$selectQuery1 = ''; // returns select statement with some conditions
$selectQuery2 = ''; // returns select statement with some conditions
$newest = $select->union([$selectQuery1, $selectQuery2]);
$subject->getSelect()->reset();
$subject->getSelect()->from(
[
'main_table' => $newest
]
);
return $proceed($printQuery, $logQuery);
So, the select statement that is returned in the variable $newest if I execute the query in mysql/phpmyadmin , I am getting the result, but in the Grid the error is throwing when customer grid opened in the admin.
Issue I found is the Union statement is wrapping wrapping with select statement. Is there any way to avoid sql wrapping or eliminate duplicate column.
Please someone look into this and provide some inputs. Thanks in advance!