Skip to content

Commit

Permalink
Merge pull request #8229 from kenjis/docs-db-event-sample
Browse files Browse the repository at this point in the history
docs: change sample code in DB Events
  • Loading branch information
kenjis authored Nov 20, 2023
2 parents abe1f59 + 247e740 commit 35e1b85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion user_guide_src/source/database/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ DBQuery
This event is triggered whenever a new query has been run, whether successful or not. The only parameter is
a :doc:`Query </database/queries>` instance of the current query. You could use this to display all queries
in STDOUT, or logging to a file, or even creating tools to do automatic query analysis to help you spot
potentially missing indexes, slow queries, etc. An example usage might be:
potentially missing indexes, slow queries, etc.

An example usage might be:

.. literalinclude:: events/001.php
16 changes: 15 additions & 1 deletion user_guide_src/source/database/events/001.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
<?php

// In app/Config/Events.php
Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');

namespace Config;

use CodeIgniter\Events\Events;
use CodeIgniter\Exceptions\FrameworkException;
use CodeIgniter\HotReloader\HotReloader;

// ...

Events::on(
'DBQuery',
static function (\CodeIgniter\Database\Query $query) {
log_message('info', (string) $query);
}
);

0 comments on commit 35e1b85

Please sign in to comment.