Product attribute join with custom table Magento 2.17 - how to retrieve custom table field
-
14-04-2021 - |
Question
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,
Solution
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.