CodeIgniter Forums
Extending \CodeIgniter\Database\MySQLi\Builder possible? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: Extending \CodeIgniter\Database\MySQLi\Builder possible? (/thread-74170.html)



Extending \CodeIgniter\Database\MySQLi\Builder possible? - tgix - 08-10-2019

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


RE: Extending \CodeIgniter\Database\MySQLi\Builder possible? - includebeer - 08-10-2019

Have you tried extending the core class like this? : https://codeigniter4.github.io/userguide/extending/core_classes.html


RE: Extending \CodeIgniter\Database\MySQLi\Builder possible? - tgix - 08-10-2019

(08-10-2019, 07:56 AM)includebeer Wrote: Have you tried extending the core class like this? : https://codeigniter4.github.io/userguide/extending/core_classes.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?


RE: Extending \CodeIgniter\Database\MySQLi\Builder possible? - includebeer - 08-10-2019

(08-10-2019, 10: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.


RE: Extending \CodeIgniter\Database\MySQLi\Builder possible? - InsiteFX - 08-11-2019

I do not see the Database Class in the list for extending system core classes.


RE: Extending \CodeIgniter\Database\MySQLi\Builder possible? - dave friend - 08-11-2019

(08-11-2019, 09: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?


RE: Extending \CodeIgniter\Database\MySQLi\Builder possible? - dave friend - 08-11-2019

(08-10-2019, 02: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


RE: Extending \CodeIgniter\Database\MySQLi\Builder possible? - kilishan - 08-11-2019

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();


RE: Extending \CodeIgniter\Database\MySQLi\Builder possible? - tgix - 08-11-2019

(08-11-2019, 09: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.