duanrenchuo9244 2016-04-11 15:51
浏览 6
已采纳

包含来自Silverstripe中外部数据库的数据的网格域

I am using a ModelAdmin to display data through gridfield. i want to manage a model in the same way, but from an external database.

In my model admin i have the code to call the model(DataObject) i want to manage:

private static $managed_models = array(
    'exampleModel'
);

To insert the data into this second database i have the follwing code inside a function:

 global $databaseConfig, $databaseConfig2;
 DB::connect($databaseConfig2); //connect to second database
 //do somee stuff
 DB::connect($databaseConfig); //return to default database

Just for test, i've used a dataObject exampleModel and defined some fields.

In this way, the gridfield is showing results from this model in my modelAdmin, so it's working, but how can i show results from my second database in this gridfield. is it possible ?

  • 写回答

1条回答 默认 最新

  • duan1226 2016-04-11 21:58
    关注

    This is an interesting scenario, one that isn't possible out of the box in a default setup of SilverStripe. Notice that at any one time, there is only one DB connection open, as is the case with most CMS systems, and it is this connection that the ORM is using in your models.

    However, I don't see in principle why this couldn't be done in SilverStripe, but there would be some additional classes needed to be written "behind" your models before it would work satisfactorily for you. Any solution would also be predicated on your wanting to edit, delete in your models (all the standard GridField actions) on each respective database.

    The only solution that comes to mind is to use SilverStripe's built-in Aspects system - but I have to admit to never having used it myself. Aspect Oriented Programming is different to standard business logic-as-class OO programming, but can still be used within an OO paradigm. It allows developers to "point cut" (to cut "horizontally" across class-logic and utilise different logic, in a different class under pre-defined scenarios).

    Think of it a little like a hook.

    Looking at the SilverStripe's aspect system and the example it uses (See: https://docs.silverstripe.org/en/3.2/developer_guides/extending/aspects/), I believe one could modify the example to your use. The example explains how, based on DB reads or writes, the ORM should use one DB connection over another, this sounds similar to your scenario, expect that instead of "read" and "write" being the arbiter of which connection is used, it would be which model - perhaps by virtue of implementing a specific interface.

    Good luck :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题