Lets asume that I have code like this:

$searchCriteria = $this->searchCriteriaBuilder
    ->addFilter('increment_id', $incrementId, 'eq')

$orderList = $this->orderRepository

Is it possible to see SQL query in a way like it was done in Magento 1 with model object:

echo Mage::getModel('module/model')->getCollection()->getSelect()->__toString();
  • 1
    try this echo $searchCriteria->getSelect()->__toString();
    – Magento 2
    Dec 13, 2017 at 9:48

3 Answers 3


You can simply do the following -:


This will output the actual query with all where clauses and joins. Hope this helps!


I just stumbled about this question by trying to find out the same on v.2.2

Unfortunately the collection of a repository isn't public accessible from within objects using it. For me the best way - for now - was to use xdebug (https://xdebug.org/) and the corresponding watcher feature of my IDE (Intellj Ultimate => see https://www.jetbrains.com/idea/). In my case I had done some filtering on the cms page repository and wanted to see the query, when it is done. I did this by adding a brakpoint in \Magento\Cms\Model\PageRepository::getList (161). When the debugger stopped there, I was able to watch the query using the watcher by adding the following expression to the watchlist:


That is the known Magento 1 style. Screenshot xdebug

I hope this helps a little bit to get further. If someone wanted to know how to use xdebug with IntelliJ or PHPStorm, have a look at https://www.ask-sheldon.com/php-xdebug/.


I would say that is because the order repository getList method returns a different object that isn't a collection ( \Magento\Sales\Api\Data\OrderSearchResultInterface ). This object returns a collection of orders inside a wrapper, with other details such as page number, total count, as well as a collection called items.

You need to create a sales order collection and then you can do


The order searchResultsInterface has a getItems method - which returns an array of order collection items:


All of the collection processing is done in this class:


You might want to test if you can log SQL from here.

  • Yes you can do it in an old fashion way but I would like to have it from repository object just to check if my search criteria are the way I want them to be in SQL statement for example SELECT * FROM table WHERE col1 = 1 AND col2 = 2 OR col3 = 3 OR col4 = 4 etc.
    – Marceli Po
    Dec 13, 2017 at 12:18
  • Understood - I have provided more info for you to trace the way it works.
    – BAF
    Dec 13, 2017 at 12:34

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.