• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacing Core Classes Note needed

#1
Hi,

Maybe it is a good idea to have a note added to the documentation regarding the replacement/extention of core Database classes.

https://www.codeigniter.com/userguide3/g...re-classes

It is misleading as you would now expect it to be possible to replace/extend Database classes
Reply

#2
There is a note where it is appropriate: https://www.codeigniter.com/userguide3/g...aries.html

Core classes are the ones located under system/core/, which the DB libs are not.
Reply

#3
I knew I had seen it somewhere, just couldn't remenber where

Adding that same note on this page can't hurt

https://www.codeigniter.com/userguide3/g...re-classes

Seeing as DB libs are also not located in ./system/libraries/
Reply

#4
You can extend the core DB libraries if you wish.

application/core/MY_Loader.php:

Code:
    /* overloaded methods */

    public function database( $params = '', $return = false, $query_builder = null ) {
        $ci =& get_instance( );

        if ( $return === false && $query_builder === null && isset( $ci->db ) && is_object( $ci->db ) && !empty( $ci->db->conn_id) ) {
            return false;
        }

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

        $db =& DB( $params, $query_builder );

        $driver = config_item( 'subclass_prefix' ) . 'DB_' . $db->dbdriver . '_driver';
        $file = APPPATH . 'libraries/' . $driver . '.php';

        if ( file_exists( $file ) === true && is_file( $file ) === true ) {
            require_once( $file );

            $dbo = new $driver( get_object_vars( $db ) );
            $db = & $dbo;
        }

        if ( $return === true ) {
            return $db;
        }

        $ci->db = '';
        $ci->db = $db;

        return $this;
    }


application/libraries/MY_DB_mysqli_driver.php:

Code:
<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class MY_DB_mysqli_driver extends CI_DB_mysqli_driver {
}

?>
Reply

#5
But you can't do that by default. You first need to extend the Loader

And when extending the Loader, or any other class, all you need to do is create a MY_xxxxx.php variant of the file and store it in the correct directory
Reply

#6
(07-20-2017, 07:23 AM)Martin7483 Wrote: But you can't do that by default. You first need to extend the Loader

Correct, the documentation states this Narf linked where above. That doesn't mean you can't only that it isn't available out of the box. The code I posted will allow you to extend them without modifying any core files.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
3 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.