Coldfusion ORM中访问者的条件 [英] conditions for accessors in Coldfusion ORM

查看:79
本文介绍了Coldfusion ORM中访问者的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦加载了组件,便可以设置条件访问该对象的属性了吗?例如,如果您在人与宠物之间存在一对多关系,请加载指定特定人的人,然后将所有提到的人宠物拉到特定种类的宠物中.例如猫与狗.

Once you have loaded a component are you then able to access properties of that object with set conditions? For instance, if you have a one-to-many relationship between people and pets, you load people specifying a particular person, you then want to pull all said persons pets where the pets are of a particular species. cats vs dogs for instance.

<cfset person=EntityLoad("person", {name="#URL.name#"})>
<cfset pets=person[1].getPets()>

总有没有要在type ='dog'之类的地方调用getPets?

is there anyway to call getPets where type='dog' or something?

还是我必须遍历宠物为每种类型创建结构并以这种方式处理它们?

Or would I have to loop through the pets creating structures for each type and deal with them that way?

推荐答案

尽可能不要遍历宠物:区分宠物的类型最好留给与它们有关系的对象或服务与那些对象一起工作.

You should not loop through the pets if at all possible: distinguishing between types of pets is best left to the object that has a relationship with them or to a service that works with those objects.

一种方法是在Person中添加一个将返回Pet类型的方法.我们在当前项目中使用的是类似的东西.

One approach is to add a method to Person that will return a type of Pet. We're using something similar in our current project.

array function getDogs() {
    var HQL = "where petType = 'dog'";
    return ormGetSession().createFilter(this.getPets(),HQL).list();
}

您也许还可以使其更通用:

You might also be able to make it more generic:

array function getPets( required string petType ) {
    var HQL = "where petType = '" & arguments.petType & "'";
    return ormGetSession().createFilter(this.getPets(),HQL).list();
}

另一种方法是在检索宠物时在Person对象中的关系中使用where属性.我们也使用这样的东西:

Another approach is to use the where attribute in relationships in the Person object when retrieving pets. We use something like this as well:

property name="cats" type="array" fieldtype="one-to-many" cfc="model.Pets" fkcolumn="PERSON_ID" where="PET_TYPE = 'cat'" lazy="false";
property name="dogs" type="array" fieldtype="one-to-many" cfc="model.Pets" fkcolumn="PERSON_ID" where="PET_TYPE = 'dog'" lazy="false";

请注意,这些示例对应用程序和数据库的结构进行了一些假设:model文件夹中有一个Pets对象,该对象映射到数据库中的表;该表具有一个PET_TYPE列(映射到petType属性),该列包含小写的宠物类型; Person对象映射到具有名为PERSON_ID的主键的表,该主键也是Pets表中的外键.

Note that these examples make several assumptions about the structure of your app and database: there is a Pets object in the model folder that maps to a table in your db; this table has a PET_TYPE column (that maps to a petType property) that contains pet types in lower case; the Person object maps to a table with a primary key called PERSON_ID, which is also a foreign key in the Pets table.

另外,在考虑这些方法和其他方法时,您应该考虑打算如何养宠物.如果您有大量Pet,特别是如果要存储多种类型,则关闭延迟加载会造成性能下降(因此,在初始化Person时要通过Pets表进行多次传递).如果您不经常访问宠物,则可能不需要单独的属性.仅具有getPets()函数可能就足够了.

Also, when considering these and other approaches, you should think about how you intend to access pets. Turning off lazy loading can create performance hits if you have a significant number of Pets, particularly if there are several types you want to store (so you're making several passes through the Pets table when initializing a Person). You may not need separate properties if you don't access Pets often; it may be enough simply to have a getPets() function.

这篇关于Coldfusion ORM中访问者的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆