0

I am trying to join customer entity table and customer address entity table. I am getting error in below code. What could be the issue

        $agency_name=$customerData->getDefaultShippingAddress()->getAgencyName();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();


        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $customerTable = $resource->getTableName('customer_entity');

$customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Address\Collection');
$collection = $customerObj
             ->addAttributeToSelect('agency_name', 'customer_address_entity')
              ->addAttributeToFilter('agency_name',array('eq'=>$agency_name));
              //->load();

              $collection->getSelect()->join(['ct'=>$customerTable],'main_table.parent_id=ct.entity_id',[]);

$c_data=$collection->getData();
print_r($c_data);

Giving me error.Please Help

1 Answer 1

1

The error is because the main_table does not exist. If you debug, you can see in

vendor/magento/module-eav/Model/Entity/Collection/AbstractCollection.php

the line

$this->getConnection()->quoteIdentifier('e.' . $attribute),

You must use

$collection->getSelect()->join(['ct'=>$customerTable],'e.parent_id=ct.entity_id',[]);

After this, it is not advisable to use the object manager as you are using it. For that, you can use the independence injections of the classes

Magento\Framework\App\ResourceConnection

and

Magento\Customer\Model\ResourceModel\Address\Collection

directly in your construct.

1
  • great.worked.Small error and took time to figure out
    – Melvin
    Commented Jul 5, 2018 at 14:00

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.