Welcome Guest, Not a member yet? Register   Sign In
Specifying Session Database
#1

[eluser]bernstable[/eluser]
Hi everyone

My application uses a couple of databases. I want to store the sessions in the database but it seems that there is no way to specify which database to store the data in. It seems to just use the default. I can specify the table but as far as I can tell specifying the database just wasn't considered.

Obviously I could hack into the CI installations sessions library and force the database, but I don't like changing the core files.

Any suggestions gratefully received.

regards

Bernie


#2

[eluser]Aken[/eluser]
You can extend the Session library just like any other library, and replace the methods that interact with the database with your own modifications (IE changing $this->CI->db to your own DB variable). Shouldn't be much work at all.

Or, just make the DB with your sessions the default. Then you don't have to do anything.
#3

[eluser]bernstable[/eluser]
Thanks for that Aken.

I have tried 'changing $this->CI->db to your own DB variable' but bizarrely whatever I try it always just uses the database that was last used prior to the session being referenced.

Is there something I'm missing with the way that the session library uses the active record class (as that is what it uses, personally I prefer long hand querying so I have little experience of active records)

Unfortunately, changing the default database isn't an option.
#4

[eluser]bernstable[/eluser]
A little more testing a little more info.

So it seems that I can easily set the database so it works for all reads in the session library but it always defaults to the last database used (outside of the session library) for writes.

So a 'hacked' solution seems to be to explicitly define the required database at the function level on the line before the writes.

I'm a little baffled as this seems wrong at so many levels. Why is it reverting to the last database used and only for writes? Weird!

If anyone has encountered this problem before and has a better solution than my hack I'd be very interested to know what it was.

Thanks

Bernie
#5

[eluser]Aken[/eluser]
Extending and modifying a library is not hacking. That's why the ability to extend libraries exists - so core files don't need to be altered. So don't worry about that.

Without seeing how you've tried to modify the library, I can't comment on why part of your code works and the other doesn't. There's nothing special about how the session library uses active record. It uses the same database connection as if you were to use $this->db in your controller or model. In other words, whichever DB connection you specify as the default in your DB config file. If you want to use a different database in your session library, you need to modify all instances of $this->CI->db to your own DB, usually done like this:

Code:
$mydb = $this->CI->load->database('dbset', true);

See "Connecting to multiple databases" for a bit more on that.




Theme © iAndrew 2016 - Forum software by © MyBB