Skip to content

Conversation

@kenjis
Copy link
Member

@kenjis kenjis commented Jun 15, 2022

Description

DBQuery
This event is triggered whenever a new query has been run, whether successful or not.
https://codeigniter4.github.io/CodeIgniter4/database/events.html#the-events

But the event DBQuery is not fired on failed query when DBDebug is true.

  • fix BaseConnection
  • fix user guide

How to Test

--- a/app/Config/Events.php
+++ b/app/Config/Events.php
@@ -46,3 +46,8 @@ Events::on('pre_system', static function () {
         Services::toolbar()->respond();
     }
 });
+
+Events::on(
+    'DBQuery',
+    static function (\CodeIgniter\Database\Query $query) { log_message('alert', (string) $query); }
+);
diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
index 7f867e95f..492606d92 100644
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -6,6 +6,8 @@ class Home extends BaseController
 {
     public function index()
     {
+        db_connect()->query('select * from bad_table where a = 1');
+
         return view('welcome_message');
     }
 }

Navigate to http://localhost:8080/ and see log file.

Before:

ERROR - 2022-06-14 22:11:50 --> Table 'ci4.bad_table' doesn't exist
CRITICAL - 2022-06-14 22:11:50 --> Table 'ci4.bad_table' doesn't exist

After:

ERROR - 2022-06-14 22:13:32 --> Table 'ci4.bad_table' doesn't exist
ALERT - 2022-06-14 22:13:32 --> select * from bad_table where a = 1
CRITICAL - 2022-06-14 22:13:32 --> Table 'ci4.bad_table' doesn't exist

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 15, 2022
@kenjis kenjis changed the title fix: trigger DBQuery is not fired on failed query when DBDebug is true fix: event DBQuery is not fired on failed query when DBDebug is true Jun 15, 2022
@kenjis kenjis added the database Issues or pull requests that affect the database layer label Jun 15, 2022
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A step in the right direction.

@kenjis kenjis merged commit ea5b283 into codeigniter4:develop Jun 15, 2022
@kenjis kenjis deleted the fix-trigger-DBQuery-on-failed-query branch June 15, 2022 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them database Issues or pull requests that affect the database layer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants