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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错