How to print searchCriteriaBuilder query in Magento 2?
-
14-04-2021 - |
Question
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());
La solution
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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange