0

I want to fetching data with customer_entity table and created custom table but gives some error in admin grid

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cus_account.customer_id' in 'on clause', query was: SELECT `main_table`.*, `customer`.* FROM `cus_account` AS `main_table` INNER JOIN `customer_entity` AS `customer` ON cus_account.customer_id = customer.entity_id LIMIT 20

This is my di.xml file

<?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="Amarnath\Cusaccount\Model\ResourceModel\Cusassign\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">cus_account</argument>
          <!--<argument name="eventPrefix" xsi:type="string">cus_account_grid_collection</argument>
            <argument name="eventObject" xsi:type="string">cus_account_collection</argument> -->
            <argument name="resourceModel" xsi:type="string">Amarnath\Cusaccount\Model\ResourceModel\Cusassign</argument>
        </arguments>
    </type>
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <!--data provider name which used in grid ui component file -->
                <item name="cusaccount_listing_data_source" xsi:type="string">Amarnath\Cusaccount\Model\ResourceModel\Cusassign\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

This is my model collection file

<?php

namespace Amarnath\Cusaccount\Model\ResourceModel\Cusassign;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init('Amarnath\Cusaccount\Model\Cusassign', 'Amarnath\Cusaccount\Model\ResourceModel\Cusassign');
    }
    protected function _initSelect()
    {
        parent::_initSelect();

        $joinTable = $this->getTable('customer_entity');
        $this->getSelect()->join($joinTable.' as customer','cus_account.customer_id = customer.entity_id', array('*'));
    }
}

1 Answer 1

0

Try this:

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

    $joinTable = $this->getTable('customer_entity');
    $this->getSelect()->joinLeft(
        ['customer' => $joinTable],
        'customer.entity_id = main_table.customer_id',
        ['*']
    );
}
2
  • This is working ... Thanks Commented Apr 20, 2023 at 9:29
  • Welcome.......... Commented Apr 21, 2023 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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