2

How to print searchCriteriaBuilder query in magento 2 ? I tried below code but it is not working.

$childProductIds = [1,2,3];
    $sortOrder = $this->sortOrderBuilder->setField('price')->setDirection('ASC')->create();

    $this->searchCriteriaBuilder->setSortOrders([$sortOrder]);
    $this->searchCriteriaBuilder->addFilter('entity_id', $childProductIds, 'in');
    
    $searchCriteria = $this->searchCriteriaBuilder->create();
    $searchCriteria->setPageSize(1)->setCurrentPage(1);
    $productList = $this->productRepository->getList($searchCriteria);
print_r($productList->getSelect()->assemble());

1 Answer 1

3

Inside of the ProductRepositoryInterface inside of the getList method, you'd do $collection->getSelectSql(true) at some point after the collection is actually loaded.

When Magento builds a product collection, it layers the filters in one at a time so the full query is not assembled until time of collection load.

You might want to do the following to force the collection to load and then print the query: $collection->getItems(); echo $collection->getSelectSql(true);

Also, you have $productList->getSelect()->assemble(). However, SearchResultsInterface does not have a getSelect method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.