This topic is locked

dynamically setting the db connection

7/5/2011 12:51:46 AM
PHPRunner General questions
P
paulirvine author

PHPRunner seems to use a single database connection (mysql user and pw) for all users who sign in. I like the simple way to associate a couple of fields from an existing table to be the username and password fields for application authentication.

But is there a way to change what database connection is used depending on a user field? or some other variable?
Or is there an event that fires when a person logs into the application that I can do other code in to set a session variable?
I'm trying to set up a multi-tenancy type of application, and so each group of users who share a tenancy need a tenantID value set on every record they add, view or edit.
Any thoughts?
many thanks

Paul

Sergey Kornilov admin 7/5/2011

You can use AfterApplicationInitialized event to override connection parameters.
Here is the sample code that points database connection to alternative database on Fridays:

$today = getdate();

if ($today['wday']==4) // Friday

{

$host="localhost";

$user="root";

$pwd="";

$port="";

$sys_dbname="anotherdb";

}
P
paulirvine author 7/5/2011



You can use AfterApplicationInitialized event to override connection parameters.
Here is the sample code that points database connection to alternative database on Fridays:

$today = getdate();

if ($today['wday']==4) // Friday

{

$host="localhost";

$user="root";

$pwd="";

$port="";

$sys_dbname="anotherdb";

}



Thanks. Does that mean that any event could also override the current connection criteria by simply setting those variables?

Your example shows it being dependent on the day which is system value, I would need to vary it dependent on user. Would the user have already logged in by this time? or i suspect the user login has not yet happened when the application initialize fires.

Is there an event after successful login but before any data is retrieved?
many thanks

Paul

Sergey Kornilov admin 7/5/2011

You can use any business logic in this event using logged user id. The very first time this event is executed user is not logged in yet. You can check if $_SESSION["UserID"] variable is populated. Is this variable is populated - user logged in.

P
paulirvine author 7/5/2011



You can use any business logic in this event using logged user id. The very first time this event is executed user is not logged in yet. You can check if $_SESSION["UserID"] variable is populated. Is this variable is populated - user logged in.


Thanks Sergey. I was just studying the events info in the help doc too. I was a little confused, but I think this helps very much. It looks like the AfterAppInit event fires on every page load, not just the Application startup. Which means I could also set a custom application session variable in the AfterSuccessfulLogin event, and in the AfterAppInit check the user is logged in, and override the connection criteria. Sounds like I need to do a test!

Thanks very much for your fast reply. Greatly appreciated.
Time to place my order and purchase!
Paul