How to extend the CodeIgniter Database Utility class

Problem

When I updated the CodeIgniter 2.2.0 application to use the database drivers mysqli, it violated the CodeIgniter backup function of the database utility class . Where I used to successfully get a database backup, now I get an error message ...

Unsupported feature of the database platform used.

The error simply means that the CodeIgniter backup function from the Utility Database class is not supported mysqli. According to online docs, only MySQL is supported.

However, I prefer not to use an outdated database driver mysql.


Possible workaround (not allowed) :

I thought I could solve this by simply expanding the CodeIgniter Database Utility class. However, CodeIgniter does not allow this according to the documentation ...

Note. Database classes cannot be extended or replaced with your own classes.


Another way (fatal error) :

EDIT: My answer and accepted answer are based on this workaround.

This GitHub page describes a method for extending a database class by creating a class MY_Loaderfor kernel extension.

https://github.com/bcit-ci/CodeIgniter/wiki/Extending-Database-Drivers

However, when I tried this solution, I got this fatal error ...

File name: core / MY_Loader.php, Line number: 49

: undefined CI_DB_mysqli_driver:: where() /home/account/codeigniter/system/libraries/Session.php 217

49: $db =& new $my_driver(get_object_vars($db));

MY_Loader , , MY_DB_mysqli_utility :

<?php class MY_DB_mysqli_utility extends CI_DB_mysqli_utility {

    function __construct($params){
        parent::__construct($params);
        log_message('debug', 'Extended DB driver class instantiated!');
    }

    function _backup($params = array())
    {
        // the working backup function is here
        .....
    }

}

( , GitHub , , - , , CI_DB_mysqli_utility CI_DB_mysqli_driver.)

EDIT: , _driver, _utility... .


( ):

, , SystemIgniter. .

"" , system/database/drivers/mysqli/mysqli_utility.php, :

function _backup($params = array())
{
    // Currently unsupported
    return $this->db->display_error('db_unsuported_feature');
}

Ellis Lab, , ...

https://ellislab.com/forums/viewthread/194645/

, _backup.

function _backup($params = array())
{
    // the working backup function is here
    .....
}

, /?

CodeIgniter mysqli, CodeIgniter Database, , . , , , , , - - , .


. , CodeIgniter v3 mysqli.

+4
4

( ) , : . *_driver *_utility.

$my_driver_file = APPPATH.'libraries/'.$my_driver.EXT; // *driver* required!!

CI_Loader::dbutil(). - :

public function dbutil()
{
    /* snip for brevity */
    $class = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_utility';
    $filename = APPPATH.'libraries/'.$class.EXT;
    require_once($filename);
    /* snip for brevity */
 }

: :)

+2

@RandomSeed...

dbutil() CodeIgniter Loader MY_Loader.

, , , , application/libraries. , , .

MY_Loader.php

<?php class MY_Loader extends CI_Loader {

    public function dbutil()
    {

        if (! class_exists('CI_DB'))
        {
            $this->database();
        }

        $CI =& get_instance();

        // for backwards compatibility, load dbforge so we can extend dbutils off it
        // this use is deprecated and strongly discouraged
        $CI->load->dbforge();

        require_once(BASEPATH . 'database/DB_utility.php');

        // START custom >>

        // path of default db utility file
        $default_utility = BASEPATH . 'database/drivers/' . $CI->db->dbdriver . '/' . $CI->db->dbdriver . '_utility.php';

        // path of my custom db utility file
        $my_utility = APPPATH . 'libraries/MY_DB_' . $CI->db->dbdriver . '_utility.php';

        // set custom db utility file if it exists
        if (file_exists($my_utility))
        {
            $utility = $my_utility;
            $extend = 'MY_DB_';
        }
        else
        {
            $utility = $default_utility;
            $extend = 'CI_DB_';
        }

        // load db utility file
        require_once($utility);

        // set the class
        $class = $extend . $CI->db->dbdriver . '_utility';

        // << END custom

        $CI->dbutil = new $class();

    }

}

application/libraries/MY_DB_mysqli_utility.php

<?php
    class MY_DB_mysqli_utility extends CI_DB_utility {

    // everything in here is same as default mysqli_utility
    ....

    // EXCEPT the _backup() function is my own

    function _backup($params = array())
    {
        //  my custom backup code
        ....

, CodeIgniter. , .


. , Utility Database CodeIgniter v3 mysqli, .

+3

.

final, ( DB.php lines 140-143):

  • system/database/drivers/, . mysupersqli/
  • mysupersqli_[driver|forge|result|utility].php
  • CI_DB_mysupersqli_[driver|forge|result|utility], CI_DB_mysqli_* (CI_DB_mysupersqli_driver::$dbdriver , )
  • , system/database/drivers/mysqli ( mysqli, )
  • config/database.php

: :)

+2
source

Since backing up db with php is limited due to memory and script timeout. How to create a cronjob to back up using mysqldump?

However, if this is not an option, perhaps this helps

0
source

All Articles