Question

Do you know of any method to show the queries being executed in Lithium as it does in CakePHP. It may become easier for me to find what is executed. At the bottom of the page it shows the queries executed.

![The screen shot] http://imgur.com/ffNfQ

After I received the answer, I added the code to my controller:

Volumes::applyFilter('find', function($self, $params, $chain) {
      echo '<pre>===== self ======<br>';
      var_dump($self);
      echo '===== params ======<br>';
      var_dump($params);
      echo '===== chain ======<br>';
      var_dump($chain);
      echo '</pre>';
      $next = $chain->next($self, $params, $chain);

      return $next;

});

It gives me an output as to all var_dump of self and params, but I require the SQL query which gets executed.

Please check the screen shot http://imgur.com/ffNfQ

Was it helpful?

Solution 3

Based on all the suggestions by Nils and Mehdi I created a fresh plugin which will only show the query results and not the vardump or print_r() for the queries executed in Lithium with MongoDB.

It really now has become easier for me to find what it being executed.

You can take a look at the project at Github:

https://github.com/nilamdoc/li3_show

Output of li3_show

OTHER TIPS

as @Nils suggested, you can leverage Lithium filters and write a simple queries logger.
I made one here. It logs the read queries to a file on a production environment.
You should be able to customize, and add filters on create, update and delete operations to fit your needs.

If you are looking to an out-of-the-box solution like Cake's debug toolbar, check the li3_perf project: https://github.com/tmaiaroto/li3_perf

You can do this by adding filters to the types of queries you wish to log. You can define them globaly in the bootstrap files our you can define them "localy" in the controllers. The example bellow does not print the query nice, however that is pretty easy to do when you have the data.

The code bellow shows how to get the information, it does not output it in a pretty way.

   Connections::get('default')->applyFilter('read', function($self, $params, $chain) {

        echo "===========</br>\n"; 
        if (is_object($params['query'])){
            var_dump($params['query']->conditions());
        }
        else{
            echo 'Query: '; var_dump($params['query']); echo "</br>\n";
        }
        $time = microtime(true);

        $next = $chain->next($self, $params, $chain);

        $time = microtime(true) - $time;            
        echo "Results: " . count((array)$next) . "</br>\n";
        echo "Time: " . $time . "</br>\n";

        return $next;

    });

Please note that echo it the data directly from the filter will render your page useless as this will be printed before headers. However if you save the data in the filter to an array instead of outputting it you can use it later in your views. And you can for instance make a template to add this information to the page.

Edit: updated code

Edit2: added a way to always get the exact SQL that is executed. To get the

Connections::get('default')->applyFilter('_execute', function($self, $params, $chain) {
    echo "SQL: " . $params['sql'] . "</br>\n";
    $time = microtime(true);

    $next =  $chain->next($self, $params, $chain);

    $time = microtime(true) - $time;
    $count = 0;
    while ($next->next())
     $count++;
    $next->rewind();
    echo "Count: " . $count . "</br>\n";        
    echo "Time: " . $time . "</br>\n";
    echo "=====================</br>\n";

    return $next;
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top