如何在 ColdFusion 中对内存中的 ORM 对象数组进行排序和过滤?

发布于 2024-12-04 00:16:14 字数 657 浏览 5 评论 0原文

假设我有一个 Store 实体,其中包含 Products 集合。因此,我像这样获取我的 StoreProducts

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左耳近心 2024-12-11 00:16:14

我认为您需要使用 HQL 来实现这一点。我有一个我之前发布的问题与按特定顺序取回物品有关。我最终创建了一个自定义的 getProducts 方法,而不是使用内置的 getter。然后,该 get 方法运行必要的 HQL 以按所需顺序返回记录。

我的问题涉及中间的连接表,但应该应用相同的概念。

public Products[] function getProducts(string SortColumn = "Name"){
    return EntityLoad("Products", {FKID = getID()}, Arguments.SortColumn & " ASC")
}

在上述情况下,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.

public Products[] function getProducts(string SortColumn = "Name"){
    return EntityLoad("Products", {FKID = getID()}, Arguments.SortColumn & " ASC")
}

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...

吲‖鸣 2024-12-11 00:16:14

您还可以通过在一对多或多对多关系的父实体上声明“where”和“orderBy”属性来指定关系集合的默认比较和排序行为。下面是一个简短的示例,后面是对支持文档的引用。

Parent.cfc

component persistent="true"
{
     property cfc="Child" fieldType="one-to-many" name="Children" where="Age >= 18" orderBy="Name ASC, Age DESC"
}

Child.cfc

component persistent="true"
{
     property cfc="Parent" fieldType="many-to-one" name="Parent"
}

参考

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

component persistent="true"
{
     property cfc="Child" fieldType="one-to-many" name="Children" where="Age >= 18" orderBy="Name ASC, Age DESC"
}

Child.cfc

component persistent="true"
{
     property cfc="Parent" fieldType="many-to-one" name="Parent"
}

Reference

Adobe ColdFusion 9 > ORM > Mapping > Define Relationship

煮茶煮酒煮时光 2024-12-11 00:16:14

没有什么比你所希望的更美好的了……天哪,我真希望有这样的事!

这是我能为你找到的最好的: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文