ColdFusion ORM:我可以指定在加载时传递给相关实体的init参数吗?

时间:2012-07-31 09:41:56

标签: orm coldfusion

假设我有一个 Page 实体,该实体可以包含一系列关联的文档实体:简单的一对多关系。

<cfcomponent entityName="Page" persistent="true" table="pages">

  <!--- A Page can have many Documents --->
  <cfproperty name="document" fieldType="one-to-many" cfc="Document" fkColumn="pageID" inverse="true">

</cfcomponent>

但是,每个Document都需要知道其文件系统目录的路径,并且此属性的值可能因上下文而异,因此它不是持久性的,需要在实例化时传入。

<cfcomponent entityName="Document" persistent="true" table="documents">

  <!--- This value needs to be set so the document knows its location --->
  <cfproperty name="directoryPath" persistent="false">

  <!--- Many Documents can belong to one Page --->
  <cfproperty name="page" fieldType="many-to-one" cfc="Page" fkColumn="pageID">

  <cffunction name="init" output="false">
    <cfreturn this/>
  </cffunction>

</cfcomponent>

如果我手动或使用Bean Factory加载页面文档数组,我可以将 directoryPath 变量指定为传递给Document init()方法的参数。但是在这里,文件的加载是由Hibernate自动完成的。

在ORM加载相关对象时,有没有办法将init参数传递给相关对象?

我知道我可以在加载后循环遍历文档并指定目录,也许这是最佳实践,但是在init上将值传递给每个文件似乎更有效。有可能吗?

1 个答案:

答案 0 :(得分:5)

通过文档查看,似乎没有办法按照你的要求行事。

我建议的一件事是,不是循环遍历文档来设置属性,而是可以在Page对象中设置属性并从Document访问它。

因此,在加载页面后,您将拥有类似Page.setDocumentPath(documentPath);的内容。

然后在显示文档时,您可以使用document.getPage().getDocumentPath();

相关问题