Database Events

The Database classes contain a few Events that you can tap into inorder to learn more about what is happening during the database execution. These events canbe used to collect data for analysis and reporting. The Debug Toolbaruses this to collect the queries to display in the Toolbar.

The Events

DBQuery

This event is triggered whenever a new query has been run, whether successful or not. The only parameter isa Query instance of the current query. You could use this to display all queriesin STDOUT, or logging to a file, or even creating tools to do automatic query analysis to help you spotpotentially missing indexes, slow queries, etc. An example usage might be:

  1. // In Config\Events.php
  2. Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');
  3.  
  4. // Collect the queries so something can be done with them later.
  5. public static function collect(CodeIgniter\Database\Query $query)
  6. {
  7. static::$queries[] = $query;
  8. }