4

I have added a new column in customer grid using the following:

app/code/Company/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <!--Custom Added to display in customer grid-->
    <preference for="Magento\Customer\Model\ResourceModel\Grid\Collection"
            type="Magento\Customer\Model\ResourceModel\Grid\Collection2" />
    <virtualType name="Magento\Customer\Model\ResourceModel\Grid\Collection2" type="Company\Module\Ui\Component\Listing\CustomerDataProvider">
        <arguments>
            <argument name="mainTable" xsi:type="string">customer_grid_flat</argument>
            <argument name="resourceModel" xsi:type="string">Magento\Customer\Model\ResourceModel\Customer</argument>
        </arguments>
    </virtualType>
    <!--Custom Added to display in customer grid End-->
</config>

app/code/Company/Module/Ui/Component/Listing/CustomerDataProvider.php

<?php 
namespace Company\Module\Ui\Component\Listing;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;

class CustomerDataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
   protected function _initSelect()
   {
      parent::_initSelect();
      $this->getSelect()->joinLeft(
        ['secondTable' => $this->getTable('custom_table')],
        'main_table.entity_id = secondTable.customer_id',
        ['customer_permissions']
      );
      return $this;
  }
}

app/code/Company/Module/view/adminhtml/ui_component/customer_listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
 <columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
    <column name="customer_permissions">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="array">  
                    <item name="notapproved" xsi:type="array">
                        <item name="value" xsi:type="number">0</item>
                        <item name="label" xsi:type="string" translate="true">Disapproved</item>
                    </item>  
                     <item name="approved" xsi:type="array">
                        <item name="value" xsi:type="number">1</item>
                        <item name="label" xsi:type="string" translate="true">Approved</item>
                    </item>
            </item>   
            <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">select</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Is Approved</item>
                    <item name="sortOrder" xsi:type="number">265</item>
            </item>
        </argument>
    </column>
  </columns>
</listing>

The custom column is added.

But while deleting the customer using mass action, I'm getting an error as:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'entity_id' in where clause is ambiguous, query was: SELECT `main_table`.`entity_id` FROM `customer_grid_flat` AS `main_table` LEFT JOIN `custom_table` AS `secondTable` ON main_table.entity_id = secondTable.customer_id WHERE (`entity_id` IN('6'))

This is the custom_table structure:

custom_table structure

The column name in table custom_table is entity_id. Also the column name in table customer_grid_flat is entity_id.

How can I solve the issue?

  • Can you post your delete Action here ? and model function too, if any. – MGento Nov 7 '17 at 4:14
  • I am getting the error while deleting the customers from grid using mass action. Hence the delete action is default customer delete mass action. – Vindhuja Nov 7 '17 at 4:24
4
protected function _initSelect()  {

  $this->addFilterToMap('entity_id', 'main_table.entity_id');
  parent::_initSelect();
  $this->getSelect()->joinLeft(
    ['secondTable' => $this->getTable('custom_table')],
    'main_table.entity_id = secondTable.customer_id',
    ['customer_permissions']
  );
  return $this; 

 }

In your app/code/Company/Module/Ui/Component/Listing/CustomerDataProvider.php file , update the _initSelect() function as above and try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.