Reputation:
I read various Q&A about "Two database for one model/controller" and I found many workaround solutions for specific cases (such as http://goo.gl/EP8BB), but I wonder if connecting ONE application to TWO databases HAS to be this difficult and time-consuming.
I mean, I have two databases (same structure, different data) but I'd like to use only one CakePHP 1.3 application. How would I choose which database connect to? It's simple, looking at the SERVER_NAME.
I have two databases in my database.php file. Why isn't possible to set something similar to this in the models?
**MyModel.php**
-----------
class MyModel extends AppModel {
if ($_SERVER['SERVER_NAME' == "app1") {
var $useDbConfig = "database1";
} else if ($_SERVER['SERVER_NAME' == "app1") {
var $useDbConfig = "database2";
}
}
Is this "quick solution" THIS naive and stupid? It seems so much easy to handle (part for the fact that I'm not yet able to make it work), instead of writing endless component, and setting datasources, etc.
Upvotes: 0
Views: 216
Reputation: 21
Given that you're putting your ifelse-Code in the __construct method of your Model (just to prevent some nasty errors filling your screen) - No, it's neither naive nor stupid.
I used sth - a little more extended but - basically similar to switch between development and production configs.
A component would make sense in my eyes if the respective config switch would be more complex and/or used in several applications.
Upvotes: 2