Welcome Guest, Not a member yet? Register   Sign In
How do I get access to the DB-object outside CI?
#1

[eluser]christian studer[/eluser]
Hello,

Is there any way to get to the database object before CI is initialised?

In my case: I'm trying to log a cache hit to the database in the output class.

Any idea?

(get_instance() is not yet available).

christian
#2

[eluser]BrianDHall[/eluser]
You could perhaps try include() on db.php in the CI library and attempting to instantiate your own DB object, its the only way I could imagine you could do it.

Rather than do all that, why not use a pre_controller hook? I think the db is available there...
#3

[eluser]Phil Sturgeon[/eluser]
DB is not available in pre_conroller and post_controller_constructor is too late. This is why I have been banging on about mid_controller_constructor.

To get the same effect as my crazy hook point you can use MY_Controller.php constructor to log this.

Other than that, you will NOT have much joy trying to run the DB class early, and will need to include the db config and use native MySQL functions.
#4

[eluser]BrianDHall[/eluser]
[quote author="Phil Sturgeon" date="1257199600"]DB is not available in pre_controller and post_controller_constructor is too late. This is why I have been banging on about mid_controller_constructor.

To get the same effect as my crazy hook point you can use MY_Controller.php constructor to log this.

Other than that, you will NOT have much joy trying to run the DB class early, and will need to include the db config and use native MySQL functions.[/quote]

Hmf, I would have thought it have been available by then.

I see what you mean, now I agree there should be a pre_constroller_constructor, but I think I understand why there isn't - if the constroller is initiated, is there really anywhere to load the hook?

Ooo, thinking outloud here, how about the Controller class constructor - in our own constructors most of us call the parent controller as the first line of the constructor, so that is the perfect place to put a hook I would think - as the last line of the controller class constructor?

But yeah, it seems MY_Controller would be the best, or just deal without having access to the DB object.
#5

[eluser]Phil Sturgeon[/eluser]
Keep up dude :-p
#6

[eluser]BrianDHall[/eluser]
[quote author="Phil Sturgeon" date="1257218215"]Keep up dude :-p[/quote]

Tongue

Seconded Wink
#7

[eluser]christian studer[/eluser]
Thanks everyone for the help, the following piece of code did it:
Code:
require_once BASEPATH.'database/DB'.EXT;
$db = DB();

Just as a side note: Controller hooks did not apply in this case as I was trying to access the DB from within the caching methods in the Output class. No controller is anywhere close to being constructed at that moment...
#8

[eluser]Phil Sturgeon[/eluser]
[quote author="christian studer" date="1257262703"]Thanks everyone for the help, the following piece of code did it:
Code:
require_once BASEPATH.'database/DB'.EXT;
$db = DB();
[/quote]

Ha! Nice hack. I'll be using that.
#9

[eluser]christian studer[/eluser]
Yeah, that's actually easier than I thought. I was afraid that I would have to instanciate a whole CI-object.

(Credit for this goes to wiredesignz over here.)




Theme © iAndrew 2016 - Forum software by © MyBB