CFWheels: Map table to another database Database.t

2019-07-14 13:25发布

I have a datasource called "cforms" which has access to two database "cforms" and "cquizes"

I wish to create the following query:

select * from cquizes.tb_depts;

I have a model for table "tb_depts":

<cfcomponent extends="Model">
    <cffunction name="init">
        <cfset table("tb_depts")>
    </cffunction>
</cfcomponent>

And my controller:

list = model("tb_depts").findAll(order="id");

When I run this controller/action. It gives me the following error:

[Macromedia][Oracle JDBC Driver][Oracle]ORA-00942: table or view does not exist

And it generates the following query:

 SELECT * FROM tb_depts

I understand what the problem is because since "tb_depts" doesn't exist in database "cforms" it throws that not found error. However is there are way to tell the model that using the datasource "cforms" access database "cquizes". For example

cquizes.tb_depts

Its seems to use the database that matches the datasource name. Is there a way to work around this functionality.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-07-14 13:34

If you need to get data from another database, There is an alternative way. For that you need to create a datasource for your second database cquizes. Then use that datasource name in the model file. This will override default datasource for that model.

For example, If you name your second datasource as cquizdatasource then in your model would be like

<cfcomponent extends="Model">
    <cffunction name="init">
        <cfset dataSource("cquizdatasource")>
        <cfset table("tb_depts")>
    </cffunction>
</cfcomponent>

Your query should work fine with the said scenario in the question. There are limitations to this, check out the link to know more.

查看更多
登录 后发表回答