File tree 9 files changed +32
-13
lines changed
user_guide_src/source/database
9 files changed +32
-13
lines changed Original file line number Diff line number Diff line change 26
26
$ dirs = [
27
27
'tests/_support/Controllers ' ,
28
28
'tests/_support/_controller ' ,
29
+ 'tests/system/Config/fixtures ' ,
29
30
];
30
31
31
32
foreach ($ dirs as $ dir ) {
Original file line number Diff line number Diff line change @@ -21,6 +21,8 @@ DBQuery
21
21
This event is triggered whenever a new query has been run, whether successful or not. The only parameter is
22
22
a :doc: `Query </database/queries >` instance of the current query. You could use this to display all queries
23
23
in STDOUT, or logging to a file, or even creating tools to do automatic query analysis to help you spot
24
- potentially missing indexes, slow queries, etc. An example usage might be:
24
+ potentially missing indexes, slow queries, etc.
25
+
26
+ An example usage might be:
25
27
26
28
.. literalinclude :: events/001.php
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
3
// In app/Config/Events.php
4
- Events::on ('DBQuery ' , 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect ' );
4
+
5
+ namespace Config ;
6
+
7
+ use CodeIgniter \Events \Events ;
8
+ use CodeIgniter \Exceptions \FrameworkException ;
9
+ use CodeIgniter \HotReloader \HotReloader ;
10
+
11
+ // ...
12
+
13
+ Events::on (
14
+ 'DBQuery ' ,
15
+ static function (\CodeIgniter \Database \Query $ query ) {
16
+ log_message ('info ' , (string ) $ query );
17
+ }
18
+ );
Original file line number Diff line number Diff line change 2
2
3
3
$ query = $ db ->query ('SELECT * FROM users; ' );
4
4
5
- foreach ($ query ->getResult (' User ' ) as $ user ) {
6
- echo $ user ->name ; // access attributes
5
+ foreach ($ query ->getResult (\ App \ Entities \ User::class ) as $ user ) {
6
+ echo $ user ->name ; // access attributes
7
7
echo $ user ->reverseName (); // or methods defined on the 'User' class
8
8
}
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
3
$ query = $ db ->query ('SELECT * FROM users LIMIT 1; ' );
4
- $ row = $ query ->getRow (0 , ' User ' );
4
+ $ row = $ query ->getRow (0 , \ App \ Entities \ User::class );
5
5
6
- echo $ row ->name ; // access attributes
6
+ echo $ row ->name ; // access attributes
7
7
echo $ row ->reverse_name (); // or methods defined on the 'User' class
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
+ namespace App \Entities ;
4
+
3
5
class User
4
6
{
5
7
public $ id ;
6
8
public $ email ;
7
9
public $ username ;
8
10
9
- protected $ last_login ;
11
+ protected $ lastLogin ;
10
12
11
13
public function lastLogin ($ format )
12
14
{
Original file line number Diff line number Diff line change 2
2
3
3
$ query = $ db ->query ('YOUR QUERY ' );
4
4
5
- $ rows = $ query ->getCustomResultObject (' User ' );
5
+ $ rows = $ query ->getCustomResultObject (\ App \ Entities \ User::class );
6
6
7
7
foreach ($ rows as $ row ) {
8
8
echo $ row ->id ;
9
9
echo $ row ->email ;
10
- echo $ row ->last_login ('Y-m-d ' );
10
+ echo $ row ->lastLogin ('Y-m-d ' );
11
11
}
Original file line number Diff line number Diff line change 2
2
3
3
$ query = $ db ->query ('YOUR QUERY ' );
4
4
5
- $ row = $ query ->getCustomRowObject (0 , ' User ' );
5
+ $ row = $ query ->getCustomRowObject (0 , \ App \ Entities \ User::class );
6
6
7
7
if (isset ($ row )) {
8
- echo $ row ->email ; // access attributes
9
- echo $ row ->last_login ('Y-m-d ' ); // access class methods
8
+ echo $ row ->email ; // access attributes
9
+ echo $ row ->lastLogin ('Y-m-d ' ); // access class methods
10
10
}
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- $ row = $ query ->getCustomRowObject (0 , ' User ' );
3
+ $ row = $ query ->getCustomRowObject (0 , \ App \ Entities \ User::class );
You can’t perform that action at this time.
0 commit comments