Does config/main.php gets reloaded after app initialization?

Hi again:

As I haven’t received any feedback on my previous post Dynamically change db connection upon login I decided to use a second db connection ‘db2’ for all models used by the tenants instead of overriding the main ‘db’ connection so both are initially configured in config/main.php. As I traced the execution flow I found that in action site/login the ‘db2’ connection is correctly set after a successful login but when the user is redirected to action site/index [via redirect(returnUrl)] the ‘db2’ connection gets reset to the default in config/main.php.

I’m not aware of any other actions being called between those 2 so I wonder if Yii somehow reloads config/main.php. Any ideas? Thanks for the help.

The main config file is included in the bootstrap and then parsed as an array and supplied with the main application class. It should never be reloaded, however, modules could choose to do so (reset data), but I’ve not encountered anything like that.

Thanks guys. The few modules I’m using are third party extensions which I don’t want to modify. If they’re reloading it, is there any way to work around it? Thanks for any ideas.

It seems quite natural that configuration gets loaded after the redirection, because the browser is going to send the server another new request. There are 2 life cycles in the sequence.

I think you can use init() method of the application base controller(usually application.components.Controller) to check if the user is logged in or not and change the db connection according to it.

Thanks @softark for the lifecycle tip; I digged into the Guide and learned about it, as previous projects didn’t require this knowledge. Indeed, the configuration is always reloaded upon each user request therefore resetting the db connection to the default.

I reckon I’ll go with overriding the onBeginRequest app behavior to set the db connection to what it should be after the user logs in.

Yep, that worked fine. Thanks!