Magento – Magento 2 : Join 2 tables in Existing collection

collection;join-tablemagento2

I've been trying to join 2 tables with the existing collection of Customers (customer_entity).

I've added like below.

$items->getSelect()->join('customer_entity_text as second', 'main_table.entity_id = second.entity_id');

Above is working fine, But if i add one more table to join with this is that is customer_entity_varchar than its not working.

Below is my code to join 2 tables.

$items->getSelect()->join('customer_entity_text as second', 
'main_table.entity_id = second.entity_id')
->join('customer_entity_varchar as third', 'main_table.entity_id = third.entity_id');

Any guide ?

*

Best Answer

Try to do with the addition of joins , example : (substitute your columns)

$items->getSelect()->join(
       ['table1join'=>$collection->getTable('customer_entity_text')],
                    'e.entity_id = table1join.product_id',
                    ['column1'=>'table1join.column1','column2'=>'table1join.column2']);
$items->getSelect()->join(
                    ['table1join'=>$collection->getTable('main_table')],
                    'e.entity_id = table1join.product_id',
                    ['table2column1'=>'table1join.column1','table2column1'=>'table1join.column2']);
$items->getSelect()->join(
                ['table2join'=>$collection->getTable('customer_entity_varchar')],
                'e.entity_id = table2join.product_id',
                ['table3column1'=>'table2join.column1','table3column1'=>'table2join.column2']);

I hope my answer brings you closer to the solution.

Related Topic