开发者

How do I sort and filter an in-memory array of ORM objects in ColdFusion?

Let's say I have a Store entity 开发者_C百科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"));


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜