Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get connection to work when using multiple keys for environments #1153

Closed
Siraris opened this issue Dec 11, 2014 · 4 comments
Closed

Comments

@Siraris
Copy link

Siraris commented Dec 11, 2014

Whenever I try to define multiple environments for my default connection, I get the following error:

PHP Fatal error: Uncaught exception 'lithium\core\ConfigException' with message 'No adapter set for configuration in class lithium\\data\\Connections

My connection is defined as follows:

Connections::add('default', array(
'development' => array(
'type' => 'MongoDb',
'host' => 'localhost',
'database' => 'dev'
),
'test' => array(
'type' => 'MongoDb',
'host' => getenv("MONGO_IPADDRESS"),
'database' => 'test'
),
'production' => array(
'type' => 'MongoDb',
'host' => getenv("MONGO_IPADDRESS"),
'database' => $_SESSION['settings']->company_short
)
));

in my connections bootstrap file. I have tried for 2 weeks to get this to work and no matter what I do, unless I hardcode the environment in a bootstrap file, it always throws the above error. If I define one default connection (Connections::add('default', array('type'=>'MongoDb', 'host'=>'localhost', 'database'=>'blah') it works fine. I'm desperate for help.

@nateabele
Copy link
Member

Most likely the environment is either not being specified, or is being specified after your application's first attempt to access the database.

To verify, call lithium\core\Environment::set("development") immediately after including libraries.php in your bootstrap. This should fix it.

For a proper implementation, see the docs for Environment::is(): http://li3.me/docs/lithium/core/Environment::is()

@Siraris
Copy link
Author

Siraris commented Dec 12, 2014

@nateabele Two things.

  1. The Environment::is with a closure never seems to work for me. I placed the following:

lithium\core\Environment::is(function($request){
$host = $request->env('HTTP_HOST');

if ($host == 'project.local' || $host == 'localhost') {
return 'development';
}
if (preg_match('/test/', $host)) {
return 'test';
}
if (preg_match('/^qa/', $host)) {
return 'qa';
}
if (preg_match('/beta/', $host)) {
return 'staging';
}
return 'production';
});

in the library bootstrap file and it never seems to fire. It seems strange to me that without that code, the framework would return an empty string for the environment. Is this normal behavior or is something else misconfigured?
2. Even though this works now when I explicitly set the environment to development in the libraries bootstrap, it seems that the Connections::add call I have above doesn't work. I have to say Connections::add("default") and Connections::add("test") in two separate calls. That nested array doesn't work. Is that antiquated? I found it http://nitschinger.at/Using-Environments-in-Lithium here I believe.

@Siraris
Copy link
Author

Siraris commented Dec 12, 2014

@nateabele So a lot of this involves trying to run a test on my users controller, and it seems that even though the tests run now without the original error, it's still not behaving as expected. When I output Environment::get() in my setUp method in my controller test, it outputs "test". When I output the available connections, it shows default and test. When I output Connections::get("test") it shows the correct connection info, but when I run my actual test (to create a user and then to remove the user collection on tearDown) it operates on the default connection settings.

I'm clearly doing something very wrong here, but based on documentation, I have no idea what more could be wrong.

@nateabele
Copy link
Member

Look at the call trace, figure out where the call to the database is coming from, and, wherever the stack leaves your code and enters the framework, log to make sure the environment is correct. If the database works without an environment configuration, then this is the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants