Reputation: 308
I want to write the form result to another database.
In Concrete 5.6 you can switch to another database on the fly. I am unable to find out how this works in Concrete 5.7.
The 5.6 way
$db = Loader::db( 'newserver', 'newuser', 'newpassword', 'newdatabase', true);
It is possible to use the legacy Loader in Concrete 5.7. I tried to connect to the database but in the error you can see that it is still trying to use the default database.
$db = Database::get();
//tried this
$db = Loader::db('localhost', 'root', 'root', 'db_new', true);
//error
//db_original.db_new does not exist
Upvotes: 2
Views: 898
Reputation: 4405
What I recommend is that you set up another connection in application/config/database.php. So your config might then look something like this:
<?php
return array(
'default-connection' => 'concrete',
'connections' => array(
'concrete' => array(
'driver' => 'c5_pdo_mysql',
'server' => 'localhost',
'database' => 'c5',
'username' => 'uuuuuuuuuuu',
'password' => 'ppppppppppp',
'charset' => 'utf8'
),
'my_new_db' => array(
'driver' => 'c5_pdo_mysql',
'server' => 'localhost',
'database' => 'db_new',
'username' => 'uuuuuuuuuuu',
'password' => 'ppppppppppp',
'charset' => 'utf8'
)
)
);
Then, when you are using your code you can access that connection at any time by doing the following:
//get the default connection
$newDb = \Core::make('database')->connection();
//get the new connection
$newDb = \Core::make('database')->connection('my_new_db');
EDIT: For those looking to change the type of database driver you will need to actually map a driver implementation in your config, which might look something like this:
<?php
return array(
'default-connection' => 'concrete',
'drivers' => array(
'pdo_sqlsrv' => 'Doctrine\DBAL\Driver\SQLSrv\Driver'
),
'connections' => array(
'concrete' => array(
'driver' => 'c5_pdo_mysql',
'server' => 'localhost',
'database' => 'c5',
'username' => 'uuuuuuuuu',
'password' => 'pppppppppp',
'charset' => 'utf8',
),
'my_new_db' => array(
'driver' => 'pdo_sqlsrv',
'server' => 'mydatabaseserver.mycompany.com',
'database' => 'my_new_db',
'username' => 'uuuuuuuuuuu',
'password' => 'ppppppppppp',
)
),
);
Upvotes: 4