5

I have created a grid with UI component with one table now I want to join another table so how can I create??

i try but not working in Vendorname\Modulename\Model\ResourceModel\Quote\collection.php

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

        $this->getSelect()->joinLeft(
                ['secondTable' => $this->getTable('quote_customer')],
                'main_table.mcs_quote_id = secondTable.quote_request_id',
                array('*')
            );
    }

enter image description here Thanks in advance.

  • what error are display? – Rakesh Jesadiya Jul 29 '17 at 6:26
  • not get any error but did not get second table filed – Sagar Dobariya Jul 29 '17 at 6:27
  • quote_customer is your custom table? Have you check value getting or not? – Rakesh Jesadiya Jul 29 '17 at 6:29
  • yes i am checking but not get value i have add screen shot please check it – Sagar Dobariya Jul 29 '17 at 6:30
  • is quote_customer your custom table? – Rakesh Jesadiya Jul 29 '17 at 6:32
8

You can display values from join two tables by using below model resource collection class function

Add below function to your model resource collection function

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

        $this->getSelect()->joinLeft(
                ['secondTable' => $this->getTable('tablename')],
                'main_table.columnname = secondTable.columnname',
                ['columnname1','columnname2','columnname3']
            );
    }

You can get more information from here magento2: admin grid showing joined table

  • hello ,i try but it is not working – Sagar Dobariya Jul 29 '17 at 6:14
  • hello,It's working on initial load page but when we try to filter out on that filed it's give me error Column not found.. – Savoo Mar 9 '18 at 9:12
  • For this we have to use addFilterToMap() in initselect() for filtering otherwise Good solution +1 – Savoo Mar 9 '18 at 9:49
  • This worked perfectly for me. Thanks! – Tony DeStefano Jul 31 at 20:40
2

Add _initSelect method to Collection.php file in Vendorname\Modulename\Model\ResourceModel\Quote\collection.php With below code

IMPORTANT: Please add addFilterToMap to initSelect Moethod Otherwise Sorting and Filtering not work with Join.

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

    $this->getSelect()->joinLeft(
            ['secondTable' => $this->getTable('tablename')],
            'main_table.columnname = secondTable.columnname',
            ['columnname1_alias'=>'columnname1','columnname2_clias''columnname2']
        );
 $this->addFilterToMap('columnname1_alias', 'secondTable.columnname1');
 $this->addFilterToMap('columnname2_clias', 'secondTable.columnname2');
    return $this;
}

Thank you

0

Try this:

protected function _renderFiltersBefore() {
        $joinTable = $this->getTable('quote_customer');

        $this->getSelect()->JoinLeft($joinTable.' as secondtable','main_table.id = secondtable.id', array('columnname'));
        parent::_renderFiltersBefore();
   }

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.