2

I want to add join query with productRepository. Here is my class. Can anyone help with this.

I have used \Magento\Catalog\Api\ProductRepositoryInterface::getList() method to get product list and now i want to add join with other table.

I am getting products data.

<?php

namespace Example\Product\Model;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderBuilder;

/**
 * Class Productlist
 *
 * @package Example\Product\Model
 */
class Productlist implements \Example\Product\Api\ProductlistInterface
{


    /**
     * Product repository
     * @var ProductRepositoryInterface
     */
    private $productRepository;

    /**
     * SearchCriteria builder
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * SortOrder builder
     * @var SortOrderBuilder
     */
    private $sortOrderBuilder;

    /**
     * ListProduct constructor
     *
     * @param ProductRepositoryInterface $productRepository
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     * @param SortOrderBuilder $sortOrderBuilder
     */
    public function __construct(
        ProductRepositoryInterface $productRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        SortOrderBuilder $sortOrderBuilder
    ) {
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->sortOrderBuilder = $sortOrderBuilder;
    }

    /**
     * Get list of products
     *
     * @return ProductInterface[]
     */
    public function getProductList($params)
    {
        // Create the SearchCriteria
        $searchCriteria = $this->searchCriteriaBuilder->create();

        // Load the products
        $products = $this->productRepository
            ->getList($searchCriteria)->setSearchCriteria();

        foreach ($products->getItems() as $product){
            echo $product->getName();
        }
        exit;
    }

}

1 Answer 1

3

I got solutions and I can able to make join using extension_attributes.xml

Created new file : etc/extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
        <attribute code="parent_product_id" type="int">
            <join reference_table="catalog_product_bundle_selection" reference_field="product_id" join_on_field="entity_id">
                <field>parent_product_id</field>
            </join>
        </attribute>
    </extension_attributes>
</config>

Now when table will automatically add to Magento\Catalog\Api\Data\ProductInterface getList()

Let me know if you have still any confusion for this.

1
  • hi dharmesh how we can use custom attributes value in reference_field
    – Hiren
    Mar 18, 2019 at 11:29

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.