Welcome Guest, Not a member yet? Register   Sign In
Changing database based on login parameter
#1

I have searched for a resolution to changing the database name based on a log in parameter and have found that it is not simple. My thought was to create a if statement in the database.php that looks for a value in a session. When the user logs in I can set the variable and use that as the database name. What I have run into is the error '$this when not in object context' when trying to use

if ($this->session->userdata['clientdb'] && $this->session->userdata['clientdb'] != ''){

When does $this start to work in relation to the session?
Reply
#2

Yeah that won't work in a config file, which gets loaded early on in the bootstrap process before CI has finished instantiating all classes used for the request.

Whats the purpose in changing the db name depending on the logged in user? Does each user have their own db or something?
Reply
#3

(This post was last modified: 06-17-2015, 04:08 PM by dutchlab.)

(06-17-2015, 03:58 PM)CroNiX Wrote: Yeah that won't work in a config file, which gets loaded early on in the bootstrap process before CI has finished instantiating all classes used for the request.

Whats the purpose in changing the db name depending on the logged in user? Does each user have their own db or something?

Yes each company will have there own database. I have a user database that will allow all users to log in and then based on their log in the database name will change.

I could put the database name in the session variable if there is a way to retrieve that value when reading the database.php.
Reply
#4

(This post was last modified: 06-17-2015, 06:53 PM by CroNiX.)

Once they are logged in you can always open a new db connection and let them use that.

http://www.codeigniter.com/user_guide/da...-databases

You also don't need to have a database config for each connection. Just pass an array of parameters to load::database()
http://www.codeigniter.com/user_guide/da...a-database
Reply
#5

(This post was last modified: 06-18-2015, 10:44 AM by dutchlab.)

This is how I have solved my issue. This allows me to set the database based on session value.

set session in auto load.
In database.php

$ci = get_instance();
if (!isset($ci->session->userdata['clientdb'])){
$ci->session->set_userdata('clientdb','database1');
}

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
........
'database' => $ci->session->userdata['clientdb'],
..........
);

When users log in successfully I set the clientdb in the session to the database name for that user. This way very time database is loaded it uses the session clientdb value for the database.
Reply
#6

hmm. Thats really interesting. Can I ask why you need to create a database for each company? And how do you create the databases on the fly? Or are you manually creating for each client?
Reply
#7

(06-22-2015, 05:46 PM)PaulD Wrote: hmm. Thats really interesting. Can I ask why you need to create a database for each company? And how do you create the databases on the fly? Or are you manually creating for each client?

Creating a database for each client is based on a possible need to separate them later.  The application I am working on can be running on the client server or ours and the separate databases make it  easier to make that switch if necessary.  The databases will be created for the clients when the account is sold but ultimately they will be created from scripts auto generate the databases.
Reply
#8

Have you tried to define different groups inside your database.php file, in addition to "default", and then load the group settings with:
PHP Code:
$this->load->database('group_name'); 
Of course, you shouldn't autoload the database driver.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB