CodeIgniter Forums
Migrating CI3 to CI4, need query->numRows function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Migrating CI3 to CI4, need query->numRows function (/showthread.php?tid=78294)

Pages: 1 2


RE: Migrating CI3 to CI4, need query->numRows function - MGatner - 01-25-2021

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/CodeIgniter4/tree/develop/system/Database/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/codeigniter4-wordpress/blob/develop/src/Database/Connection.php


RE: Migrating CI3 to CI4, need query->numRows function - plaztic - 01-25-2021

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;
    }

}