如何为 ORM 设置动态数据源?
Coldfusion application.cfc 中的 ORM 设置在其他任何内容运行(onapplicationstart 等)之前运行。那么如何在 application.cfc 中设置动态数据源(ORM init 之前的代码)?我们可以在之后设置它,并将 ORM 重新指向动态数据源,但这要求硬编码数据源也必须有效。这充其量是脆弱的。
下面是一个示例:
<cfscript>
this.name = "someapp_#hash(cgi.http_host)#";
this.ormenabled = "true";
this.ormsettings = { cfclocation = "config/definitions", eventhandling = "true",datasource="STATICDATASOURCE" };
</cfscript>
如果未在 application.cfc 范围中指定,则会出现“未为当前应用程序配置 ORM”之类的错误。
我们需要能够从服务器上的文本文件获取数据源。
ORM settings in Coldfusion application.cfc run before anything else runs (onapplicationstart, etc). So how do you set a dynamic datasource (code before the ORM init) in application.cfc? we can set it after and it re-points the ORM to a dynamic datasource, but that requires that the hardcoded datasource must be valid as well. This is tenuous at best.
Here is an example:
<cfscript>
this.name = "someapp_#hash(cgi.http_host)#";
this.ormenabled = "true";
this.ormsettings = { cfclocation = "config/definitions", eventhandling = "true",datasource="STATICDATASOURCE" };
</cfscript>
If it's not specified in application.cfc scope then you get errors like "ORM is not configured for the current application."
We need to be able to get the datasource from a text file on the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果您想存储一个文件,在本例中我们将其称为“datasource.xml”,其中包括:
您可以使用以下命令读取它:
Well, if you wanted to store a file, for this example we'll call it "datasource.xml" consisting of:
You can read it in with:
如果未定义,ORM 数据源仅使用默认数据源。
话虽如此,如果您想动态添加/删除数据源,请参阅管理员 API:http://help.adobe.com/en_US/ColdFusion/9.0/Admin/WSc3ff6d0ea77859461172e0811cbf364104-7fcf.html(自 CF8 起可用)
我不确定您是否可以重新设置
this.ormsettings.datasource
在运行时更改为其他内容(即onApplicationStart()
?或onServerStart()
?),但许多设置可以再次设置。您可能想尝试一下。ORM datasource just uses the default datasource if not defined.
Having said that, if you want to add / remove datasource dynamically, see Administrator API at: http://help.adobe.com/en_US/ColdFusion/9.0/Admin/WSc3ff6d0ea77859461172e0811cbf364104-7fcf.html (available since CF8)
I'm not sure if you can re-set the
this.ormsettings.datasource
to something else at runtime (i.e.onApplicationStart()
? oronServerStart()
?), but many of the settings can be set again. You may want to try it out.