Tuesday, May 25, 2010

Multiple connections

Staff,
I have a database that has a table that contains the data for
several other connections to the database.

When I have a SQL script (example: ALTER TABLE ...) I want to run for
all other databases.
I would read the main database and I would connect to other databases
to run the script.

How can I make a loop (execution time) I log in other databases in
order to run the script?

I found a way: (the $ databases array contains the connection data
already consulted)
foreach($databases as $k=>$database){
$connection = array(
'driver' => 'mysql',
'persistent' => false,
'host' => $database['Database']['host'],
'port' => $database['Database']['port'],
'login' => $database['Database']['login'],
'password' => $database['Database']
['password'],
'database' => $database['Database']
['database'],
);

$db =& ConnectionManager::getDataSource("default");
$db->reconnect($connection));

$scripts = explode(';', $script);
foreach($scripts as $sql){
if(!empty($sql)) $this->Database->query($sql);
}

}
But I'd like a way that works in the same pattern of Cake.
Putting even at runtime the $this->Model->useDbConfig =
'name_of_the_connection';

But then I'd have to Savar an array for each connection in the file
config\database.php

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: