SilverStripe 将数据保存到外部 table

SilverStripe save data to external table

在 SilverStripe 中,如何将数据保存到外部数据库 table,它不是由 SilverStripe 创建的?

例如: 我创建了一个 News table 并希望 - 如果我添加新的新闻项目 - 相同的数据存储在我以前的新闻 table.

如果表相同,您实际上可以切换数据库配置,然后使用 ORM,然后切换回来...

$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);

我会创建一个 restful API 来保持独立,因此您不需要在 SilverStripe 站点中保留外部数据库凭据。

例如,您可以使用 https://github.com/guzzle/guzzle 将新闻从您的 SilverStripe 站点 (onAfterWrite) POST 发送到外部站点(外部数据库所在的位置)。在外部站点上,您需要创建一个简单的 API 服务器来侦听 Post 请求,如果有效则将它们保存在数据库中。 sql 次注射有什么用!

希望对您有所帮助。