Welcome Guest, Not a member yet? Register   Sign In
Override database method
#1

Hi everybody \o
I'm working in a feature that requests the change of database at execution time. I already read and search a lot,without progress. Actually I'm making a override from a core method(database - in core/Loader.php) with a MY_Loader class and call it from a hook.

What happens next is the problem. The params are not accepted,it's like I  had not pass it to the function call. I have added a fourth param,to force the reload. Any help would be appreciated.

MY_Loader.php
Code:
function database($params = '', $return = FALSE, $active_record = NULL,$force_load = FALSE) {
       // Grab the super object
       $CI =& get_instance();
       
       // Do we even need to load the database class?
       if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db) AND $force_load == FALSE)
       {
               return FALSE;
       }

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

       if ($return === TRUE)
       {
               return DB($params, $active_record);
       }
       
       // Initialize the db variable.  Needed to prevent
       // reference errors with some configurations
       $CI->db = '';
       
       // Load the DB class
       $CI->db =& DB($params, $active_record);
   }


Hook to change the Database - It's post_controller_constructor
Code:
class ChangeDatabase{
   
   function changeToRead(){
       $CI =& get_instance();
       
       if($CI->session->userdata['usercode'] !== $CI->session->userdata['usercodeloged']){
             $CI->load->database("canOnlyRead",true,null,true);
       }else{
           $CI->load->database('default');
       }
   }
}
Reply
#2

The only time you're calling your method with the extra parameters, you're passing true as the second parameter, which means you are returning the result of calling DB($params, null), but you aren't saving the result, so $CI->db doesn't change and you don't have a reference to the DB class you just loaded.

Further, when you pass true as the second parameter, there's no real difference between your database() method and the CI2 loader's database() method, because the check under the comment "Do we even need to load the database class?" will skip the "return FALSE;" line once it sees that $return is true.

It would be cleaner to do something like this:
PHP Code:
class MY_Loader extends CI_Loader
{
    function 
database($params ''$return FALSE$active_record NULL$force_load FALSE) {
        if (
$force_load !== TRUE OR $return === TRUE) {
            return 
parent::database($params$return$active_record);
        }

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

        
// Grab the super object
        
$CI =& get_instance();

        
// Initialize the db variable.  Needed to prevent
        // reference errors with some configurations
        
$CI->db '';

        
// Load the DB class
        
$CI->db =& DB($params$active_record);
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB