CodeIgniter Forums

Full Version: extending the database class in a proper way
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]xeroblast[/eluser]
just wanted to share it. because all i see are hacking the codeigniter core. and the problem on hacking its core is when you try to update your codeigniter and your application went nuts..

let start by knowing/setting your subclass_prefix in "application/config/config.php" e.g ("MY_"). so every filename of your subclass start with that prefix

we create your subclass loader in "application/core" directory and name it "MY_Loader.php" and copy the function ( "function database()" ) of the original Loader.php in "system/core/Loader.php" to your "MY_Loader.php"
find and change the code according below -- dont forget to extend your class "extends CI_Loader"
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Loader extends CI_Loader {
...
//require_once(BASEPATH.'database/DB.php'); // original Loader.php
require_once(APPPATH.'database/MY_DB.php'); // our subclass
...
}

we create a folder named "database" then we copy the file "system/database/DB.php" to our newly created directory "application/database" and then we renamed it "MY_DB.php". open "MY_DB.php", find and replace/comment out the code according below
Code:
/** comment out this original code
if ( ! class_exists('CI_DB'))
{
eval('class CI_DB extends CI_DB_active_record { }');
} **/
/** this will load your custom active record class **/
$custom_active_record = file_exists( APPPATH.'database/'.config_item('subclass_prefix').'DB_active_record.php' );
if ( $custom_active_record ) {
require_once(APPPATH.'database/'.config_item('subclass_prefix').'DB_active_record.php');
}
if ( ! class_exists('CI_DB'))
{
if ( $custom_active_record ) {
  eval('class CI_DB extends '.config_item('subclass_prefix').'DB_active_record { }');
} else {
  eval('class CI_DB extends CI_DB_active_record { }');
}
}
...
// Instantiate the DB adapter
$driver = 'CI_DB_'.$params['dbdriver'].'_driver';

/** insert code for loading your custom database driver class **/
if ( file_exists( APPPATH.'database/drivers/'.$params['dbdriver'].'/'.config_item('subclass_prefix').'DB_'.$params['dbdriver'].'_driver.php' ) ) {
require_once( APPPATH.'database/drivers/'.$params['dbdriver'].'/'.config_item('subclass_prefix').'DB_'.$params['dbdriver'].'_driver.php' );
$driver = config_item('subclass_prefix').'DB_'.$params['dbdriver'].'_driver';
}
/** end of inserted code **/

$DB = new $driver($params);

now you're almost done...
so when extending the active record class. create the file "MY_DB_active_record.php" in "application/database" directory. ( "application/database/MY_DB_active_record.php" ) -- dont forget to extend your class "extends CI_DB_active_record"
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_DB_active_record extends CI_DB_active_record {
public function __construct($params)
{
     parent::__construct($params);
     log_message('debug', 'Extended DB active record class instantiated!');
}
public function get_compiled_select($table = '', $reset = TRUE, $from = TRUE)
{
if ($table != '')
{
     $this->_track_aliases($table);
     $this->from($table);
}
$select =  $this->_compile_select( FALSE, $from );
if ($reset === TRUE)
{
     $this->_reset_select();
}
  return $select;
}
}

and when extending the driver class, create a directory in "application/database" named "drivers". and inside "drivers" directory create another directory with database driver name (e.g. "mysql", "mssql", "postgre"). and inside the database driver name directory, create the driver file "MY_DB_mysql_driver.php". ( "application/database/drivers/mysql/MY_DB_mysql_driver.php" - for mysql ) -- dont forget to extend your class "extends CI_DB_mysql_driver"
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_DB_mysql_driver extends CI_DB_mysql_driver {
    public function __construct($params)
    {
     parent::__construct($params);
     log_message('debug', 'Extended DB driver class instantiated!');
    }
    function union_from()
    {
     echo 'custom test';
    }
    function union_select()
    {
     echo 'custom test';
    }
}