Welcome Guest, Not a member yet? Register   Sign In
Database access before controllers / configs load
#1

[eluser]haraldo[/eluser]
Hi there,

I have a pre-controller hook that instantiates a custom session library class. Once instantiated i want to log some data. Problem is the database library doesn't exist yet. So how can i access the database at this stage. If i don't log stuff here ( in my session class ) and put the log method in a controller it logs every time the controller class is instantiated. That's not good for me.

Thanks for any advice.
#2

[eluser]Frank Berger[/eluser]
you'd have to open a second db-connection in the hook or with a hook. I used the following code before, registered as pre_system and post_system hook:

file hooks/hookdb.php
Code:
if ( ! function_exists('hookdb_open')) {
    function hookdb_open() {
        global $HOOKDB;
        require_once(BASEPATH.'database/DB'.EXT);
        $HOOKDB = DB();
    }
}

if ( ! function_exists('hookdb_close')) {
    function hookdb_close() {
        global $HOOKDB;
        $HOOKDB->close();
    }
}

file config/hooks.php
Code:
$hook['pre_system'][] = array('function'=>'hookdb_open','filename'=>'hookdb.php','filepath'=>'hooks');
$hook['post_system'][] = array('function'=>'hookdb_close','filename'=>'hookdb.php','filepath'=>'hooks');

you can access the database with $GLOBALS['HOOKDB']. Not sure right now, but I think in that setup you only have standard DB functionality, and no active records.

I used that in an unpublished package to overwrite settings in the config/*.php files from a database.

Frank
#3

[eluser]haraldo[/eluser]
Many thanks Frank. Looks good to me.

I could just use this DB connection as my primary connection and leave it at that, right?

You'd think there would be a standard way of creating this in codeigniter. A way to instantiate a DB before configs / controllers are loaded. Who knows.

Not too fussed about the active record functionality. I'll test it out though.




Theme © iAndrew 2016 - Forum software by © MyBB