SilverStripe save data to external table

烂漫一生 提交于 2019-12-11 09:13:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!