如何在 ColdFusion 中对内存中的 ORM 对象数组进行排序和过滤?
假设我有一个 Store
实体,其中包含 Products
集合。因此,我像这样获取我的 Store
和 Products
:
var store = entityLoadByPK("Store", 13);
var products = store.getProducts();
现在我想对 Products
进行排序和过滤,此时它是一个 in-内存收集(假设代理已被解析)。这在 ColdFusion 中可能吗?如果可以,我该怎么做?
旁注:我基本上是在寻找类似于 C# LINQ 功能的东西,我可以这样做:
var store = session.Query<Store>().Single(x => x.Id == 13);
var products = store.GetProducts();
var sortedProducts = products.OrderBy(x => x.Name).ToList();
var filteredProducts = products.Where(x => x.Name.Contains("Shampoo"));
Let's say I have a Store
entity that contains a collection of Products
. So I grab my Store
and Products
like this:
var store = entityLoadByPK("Store", 13);
var products = store.getProducts();
Now I'd like to sort and filter Products
, which at this point is an in-memory collection (let's assume that the proxies have been resolved). Is this possible in ColdFusion, and if so, how would I do it?
Side note: I'm basically looking for something similar to C# LINQ's features, where I can do:
var store = session.Query<Store>().Single(x => x.Id == 13);
var products = store.GetProducts();
var sortedProducts = products.OrderBy(x => x.Name).ToList();
var filteredProducts = products.Where(x => x.Name.Contains("Shampoo"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要使用 HQL 来实现这一点。我有一个我之前发布的问题与按特定顺序取回物品有关。我最终创建了一个自定义的 getProducts 方法,而不是使用内置的 getter。然后,该 get 方法运行必要的 HQL 以按所需顺序返回记录。
我的问题涉及中间的连接表,但应该应用相同的概念。
在上述情况下,FKID 是产品表中的外键列,而 getID 是 Store 对象中 PK 的默认 getter。
我更新了答案以允许您也传递排序值:)。一定要保持它的华丽...
I think you'll need to use HQL to make that happen. I have a question I posted some time back that had to do with getting items back in a specific order. I ended up creating a custom
getProducts
method, instead of using the built in getter. That get method then ran the necessary HQL to return the records in the order required.My question involves a join table in the middle, but the same concepts should apply.
In the above case, FKID is the foreign key column in your products table, and getID is the default getter for the PK in your Store object.
I updated the answer to allow you to pass in the sort value as well :). Gotta keep it fancy...
您还可以通过在一对多或多对多关系的父实体上声明“where”和“orderBy”属性来指定关系集合的默认比较和排序行为。下面是一个简短的示例,后面是对支持文档的引用。
Parent.cfc
Child.cfc
参考
Adobe ColdFusion 9 > ORM>测绘>定义关系
You may also specify the default comparative and sorting behavior of a relational collection by declaring the "where" and "orderBy" property attributes on the parent entity of a one-to-many or many-to-many relationship. Below is an abbreviated example, followed by a reference to the supporting documentation.
Parent.cfc
Child.cfc
Reference
Adobe ColdFusion 9 > ORM > Mapping > Define Relationship
没有什么比你所希望的更美好的了……天哪,我真希望有这样的事!
这是我能为你找到的最好的:http://cookbooks.adobe.com/post_How_to_sort_an_array_of_objects_or_entities_with_C-17958.html
There's nothing like what you're hoping for... Boy do I wish there was!
Here's the best I can find for ya: http://cookbooks.adobe.com/post_How_to_sort_an_array_of_objects_or_entities_with_C-17958.html