Welcome Guest, Not a member yet? Register   Sign In
Migrating CI3 to CI4, need query->numRows function
#11

(This post was last modified: 01-25-2021, 07:02 AM by MGatner.)

For the record a simple way to extend the Database classes is to create your own driver, which can extend any current driver with whatever modifications you need. For example you could create app/Database/MyDriver/Connection.php with:
PHP Code:
class Connection extends \CodeIgniter\Database\MySQLi\Connection

and then use it in app/Config/Database.php (or .env):
PHP Code:
    /**
     * The default database connection.
     *
     * @var array
     */
    
public $default = [
...
        
'DBDriver' => 'App\Database\MyDriver',
...
    ]; 


Your driver needs to support every class (see the list here https://github.com/codeigniter4/CodeIgni...ase/MySQLi) but they can be empty class extensions or even just class aliases. You can see my class alias approach for my WordPress database driver, which is a simple extension of the core MySQLi:
https://github.com/tattersoftware/codeig...ection.php
Reply
#12

You can create a helper for ease of use:

Code:
if (!function_exists('numRows'))
{
    /**
     * Accepts database result object and returns number of rows found.
     * CI 4 does not have CI 3 method $query->num_rows() thus the need for this function.
     *
     * @param obj $dbObject The returned database object from running query.
     *
     * @return int The row count.
     */
    function numRows($dbObject): int
    {
        return $dbObject->resultID->num_rows;
    }

}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB