Coldfusion 9 ORM映射问题

5

我有一个与CF9 ORM映射相关的问题。

有时候会出现以下错误(是的,大部分时间都能正常工作),

Mapping for component model.Pubs not found. Either the mapping for this component is missing or the application must be restarted to generate the mapping.

在Application.cfc中定义ORM

    <cfscript>
    this.datasource = "Pubs";
    this.ormenabled = true;
    this.ormsettings= {
                        dialect="MicrosoftSQLServer",
                        dbcreate="update",                              
                        eventhandling="true"
                    };      
</cfscript>

<cfset this.mappings["/model"] = getDirectoryFromPath(getCurrentTemplatePath()) & "model" />

唯一的解决方法是多次刷新ORM,通过在Application.cfc上加上?init=true来实现。这仍然是一个临时解决方案,但我需要知道它的根本原因并加以修复。

<cfscript>          
if(structKeyExists(url, "init")) { ormReload(); applicationStop(); location('index.cfm?reloaded=true'); }

请给予建议。

谢谢!


1
尝试在你的ormsettings中设置cfclocation,这样你就不需要在初始化中使用applicationStop - Henry
@Henry 谢谢,我试过 cfclocation 但它不能与我的模型映射一起使用。也许这就是原因。我会在服务器上尝试并看看它是否能够工作。 - John N
2个回答

1

好的,感谢@Henry和@Walter的评论。它们指引了正确的解决方案。

以下是我所做的,以确保它始终稳定。

  1. 我将所有ORM CFCs放在一个文件夹(位置)中。以前每个部分都有一个“model”文件夹。这些部分是根目录下的兄弟文件夹,并共享同一个Application.cfc。 我将其更改为所有CFC的一个根级文件夹,即:/root/ormmodel
  2. 在/root/Application.cfc上,我调整了以下代码

    <cfset application.mappings["/ormmodel"] = expandPath("/root/ormmodel") />
    

    this.ormsettings= {
        cfclocation = ["ormmodel"],
        autogenmap = true,
        ...
        eventhandling="true"                            
    };  
    

    请注意cfclocation值中缺少的“/”。

  3. 在调用模型组件时,我将代码从pub = new ormmodel.Pubs()更改为

    pub = EntityNew("Pubs");
    
  4. 在一个无关的点上,我已将我的组件名称更改为驼峰命名法,并避免使用下划线和破折号等特殊字符。

希望这篇文章能够帮助到其他人,节省他们数小时的挫败和悬念。

愉快编码!


1
我之前也遇到过你的问题,但现在它已经正常工作了。首先,如果你没有设置ormsettings.cfclocation,ColdFusion会这样做:

如果没有设置,ColdFusion会查找应用程序目录、其子目录和映射目录以搜索持久CFC。(参见规范

这是容易出错的,因为你永远不知道ColdFusion在所有这些目录中找到了什么。
当你将cfclocation添加到你的示例中时,它应该可以工作:
this.ormsettings= {
    cfclocation = ["/model", "/other/entities", "/more/other/entites"]
}

关于如何指定cfclocation的路径,有很多讨论。对我来说,这种方式可行。

但是我的cfclocation的第一个元素总是应用程序映射,例如this.mappings ["/model"]。我没有在没有映射的Web服务器别名或Webroot中测试过它是否有效。您还应避免命名空间冲突,例如在Webroot中具有“model”目录,同时具有“/model”映射。

祝你好运:)


嘿,沃尔特,非常感谢。我试了一下,让我们等着瞧吧。到目前为止,它运行良好。我希望这是一个稳定的替代方案,可以取代我之前的配置。 - John N

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接