This topic is locked
[SOLVED]

 Multiple databases setup

4/20/2020 4:31:41 PM
PHPRunner General questions
J
Jack72 author

Hi All,
Can anyone help me out with the following:
I setup project with a database for administration of customers and their users.

They login according to the rights as set in the admin database/user table.
After login they are connected to their own database. This works ok.
I want to get some data from the 'customer' database. Which event can I use?
I tried 'AfterSuccessfulLogin' but it doesn't work as expected:

$srs = DB::Query("select * from settings");

while ($sdata = $srs->fetchAssoc()) {

$_SESSION["OrderTableId"]=$sdata["OrderTableId"];

}


$_SESSION["OrderTableId"] is empty after this.
The same result when I use AfterAppInit.
Should I use a different event? Or do I have to force a new connection?
Any help is welcome!

Admin 4/20/2020

You can use any event but it is not easy to understand what exactly the question is.
If you are asking how to retrieve data from the database connection that is not a primary database connection - check this article:

https://xlinesoft.com/phprunner/docs/db_setconnection.htm
Note that the secondary connection needs to be added to PHPRunner first.
Also if you are building a SaaS multi-tenant kind of application - check this:

https://xlinesoft.com/blog/2019/10/03/saas-application-design/

J
Jack72 author 4/20/2020

The setup as described in the article you mention was followed and works fine:

If I set the $_SESSION['dbname'] to the 'customer' database, I see records from that database on the different list,add and viewpages.
The question is: at which moment is the connection switched to the 'customer' database?

Is it directly after I set the $_SESSION variable? or do I need to set the connection manually.
If I select data from the 'customer' database in the AfterSuccessfulLogin event, right after setting $_SESSION['dbname'] it doesn't work. I get an empty recordset.

Doing the same query in the BeforeProcessRowList event, it does work.
So I try to understand where and when I can select data from the customer-database to set some session variables. To me it would be most logical in the aftersuccessfullogin event.

J
Jack72 author 4/21/2020

After an 'upload all files' to the server and moving the DB::Query to the AfterAppInit event, it seems to work stable now.

I was expecting that the event AfterSuccessfulLogin was fired on every new opening of the webapp. I found out that the AfterAppInit event is fired everytime the user refreshes his page.
Thanks for creating a great tool Sergey, keep up the good work!