问题
In SilverStripe how can I save data to an external database table, which has not been created by SilverStripe?
For example:
I have created a News
table and want - if I add new a News item - that the same data is stored in my previous News table.
回答1:
If the tables are the same you can actually just switch the database config and then use the ORM and then switch is back...
$otherDB = array(
"type" => 'MySQLDatabase',
"server" => 'localhost',
"username" => 'new_user',
"password" => 'xxxx',
"path" => '',
"database" => 'new_database'
);
DB::connect($otherDB);
//Use ORM to write
//revert to normal credentials
global $databaseConfig;
DB::connect($databaseConfig);
回答2:
I would create a restful API to keep things separate, so you don't need to keep the external db credentials in your SilverStripe site.
You can use https://github.com/guzzle/guzzle to POST the News to the external site (where the external db lives) from your SilverStripe site (onAfterWrite) for example. On the external site you'll need to create a simple API server that listens to Post requests and if valid saves them in the db. What out for sql injections!
hope it helps.
来源:https://stackoverflow.com/questions/32582566/silverstripe-save-data-to-external-table