0

In my code I have seller_id as the product attribute and the seller information are there in a custom table (lof_marketplace_seller). I want to join the product collection with this custom table. I did the join this way:

$collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->load();
 $collection->getSelect()->join(

                ['lof_marketplace_seller'],
               'lof_marketplace_seller.seller_id = e.seller_id',
                []


        )->columns(['seller_name'=> 'lof_marketplace_seller.name']);

and then I write this to get the seller name:

foreach ($collection as $value) {
    $sellerName = $value->getData('seller_name');
    ................
}

But I'm not able to get seller name by using the code. When I try to generate the SQL from the $collection, I'm able to get the fields & values exactly. (by $collection->printlogquery(true))

My question is how to retrieve the seller_name from the result set. OR simply how to retrieve custom table field in a JOIN operation

$productCollection Variable:

class Json extends \Magento\Framework\App\Action\Action {
    private $productCollection;
    public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \ESPL\Autocomplete\Helper\Data $helper,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
    \Magento\Store\Model\StoreManagerInterface $store,
    \Magento\Catalog\Helper\Image $image,
    \Magento\Catalog\Model\ProductFactory $product
)
{
    parent::__construct($context);
    $this->resultPageFactory = $resultPageFactory;
    $this->helper = $helper;
    $this->productCollection = $productCollection;

}

public function execute()
{ 

   $productCollection = $this->productCollection;
   $collection = $productCollection->create()
   ............................

Thanks,

9
  • 1
    print $collection object and see, you are getting seller_name or not Sep 19, 2017 at 14:07
  • When I print_r($collection), it outputs a long object array. Actually we can get the name, entity_id etc from $value->getData() function. But seller_name is not getting. Also it is not throwing any error
    – Hari
    Sep 19, 2017 at 14:36
  • 1
    That means your joining query is incorrect. Sep 19, 2017 at 14:36
  • No I'm able to generate the SQL using $collection->printlogquery(true) and able to run the SQL in my phpmyadmin. It shows output
    – Hari
    Sep 19, 2017 at 14:38
  • Can you please update your code about $productCollection variable? Sep 19, 2017 at 14:47

1 Answer 1

0

Finally I got the answer!. Thanks to Land of coders https://landofcoder.ticksy.com/ticket/1307647

this is the answer:

foreach ($collection->getData() as $value) {    
   $sellerName = 
 $value['seller_name'];    ................

}

Can anybody suggest me links or tutorial which give the good documentation for customizing magento especially what are its inner functions and how to use them for common customization purposes. It is very hard to customize magento.

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.