Welcome Guest, Not a member yet? Register   Sign In
Extending \CodeIgniter\Database\MySQLi\Builder possible?
#1

(This post was last modified: 08-10-2019, 01:42 AM by tgix.)

More questions on CI3 -> CI4...
In my old project I use SQL_CALC_FOUND_ROWS to query the database for the total count of records while doing a WHERE/LIMIT query. I need to LIMIT the queries to keep the frontend UI happy with huge datasets. I should only insert SQL_CALC_FOUND_ROWS if there is a WHERE clause in the SQL query, otherwise the query will be slow and a COUNT(*) is better.

In CI3 I could inject the SELECT directly into the $db object, but that's not the case in CI4 where the $db is not global. Rather than passing around $db I wonder if it is possible to extend \CodeIgniter\Database\MySQLi\Builder with my own function to add a function so I could do something like this:
PHP Code:
$query $builder
    
->calc_found_rows()                // Should check for filter in the request and SQL_CALC_FOUND_ROWS
    
->select('userID,firstname,lastname,email')
    ->
add_request_filters()            // Another function I'd like to add
    
->get($this->limit$this->start); // Paging parameters set in the extended Controller
        
    
$total $builder->get_total_count(); // If SQL_CALC_FOUND_ROWS used, get FOUND_ROWS() else COUNT(*) 

I have tried adding a class Builder.php to app/Database/MySQLi extending \CodeIgniter\Database\MySQLi\Builder but it doesn't load.

I know that I can hack the class in system but that sort of defeats the purpose of having composer-supported frameworks.

Thanks,
/Mattias
Reply
#2

Have you tried extending the core class like this? : https://codeigniter4.github.io/userguide...asses.html
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(08-10-2019, 06:56 AM)includebeer Wrote: Have you tried extending the core class like this? : https://codeigniter4.github.io/userguide...asses.html

Yes, I put a file Builder.php in app/Database/MySQLi directory, extending \CodeIgniter\Database\MySQLi\Builder. I have also tried putting it in app/Library. Should I have put it elsewhere?
Reply
#4

(08-10-2019, 09:14 AM)tgix Wrote: Yes, I put a file Builder.php in app/Database/MySQLi directory, extending \CodeIgniter\Database\MySQLi\Builder. I have also tried putting it in app/Library. Should I have put it elsewhere?

I have no idea. I never extended core classes in CI4 yet.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

I do not see the Database Class in the list for extending system core classes.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(08-11-2019, 08:23 AM)InsiteFX Wrote: I do not see the Database Class in the list for extending system core classes.

Where is this list of which you speak?
Reply
#7

(This post was last modified: 08-11-2019, 10:30 AM by dave friend.)

(08-10-2019, 01:42 AM)tgix Wrote: I have tried adding a class Builder.php to app/Database/MySQLi extending \CodeIgniter\Database\MySQLi\Builder but it doesn't load.

Have you tried modifying /app/Config/Services.php to load your extending class in place of the core class?

Services
Reply
#8

First - extending database classes doesn't work, unfortunately. It's gets messy really fast due to the way the classes are found, so you'd have to basically extend ALL of the MySQLi classes and make it a separate driver. Probably not what you're looking for.

More importantly, though, by default the database connection is shared so no matter how times you call db_connect(), or go directly through CodeIgniter\Database\Config::connect(), you'll get the same instance of the database connection. You always have the option of getting a new instance but the default is to share the connection.

Also, you can always get the last query ran, with

Code:
$query = db_connect()->getLastQuery();

That will return a Query object that you can get the query string and check if a where is in it. You can cast that object as a string to get the final query, or call $query->getQuery();
Reply
#9

(08-11-2019, 08:43 PM)kilishan Wrote: First - extending database classes doesn't work, unfortunately. It's gets messy really fast due to the way the classes are found, so you'd have to basically extend ALL of the MySQLi classes and make it a separate driver. Probably not what you're looking for.

Thanks! I know from CI3 that extending Database was ugly. I didn't understand the shared $db object so thanks for pointing that out.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB