Filtering associated entities

I have an entity called ForumTopic

This has a one to many relationship with ForumReplies.

I want to get all Replies from the topic that have a property ‘enabled’ set to true, but I can’t quite work out the best way to do this.

I thought the best way was to put a method in my ForumReplies entity and use the Coldbox ORM findAllWhere method, but then I can’t filter on the topic, because topicid is not a field, topic is an association. Here is my entity with the getPublishedReplies() method.

`

component persistent=“true” table=“forumtopics” hint=“Forum Topics” extends=“coldbox.system.orm.hibernate.ActiveEntity”{

property name=“id” fieldtype=“id” generator=“native”;

property name=“orgID” ormtype=“integer”;

property name=“groupID” ormtype=“ineger” dbdefault=“0” default=“0”;

property name=“title” ormtype=“string” length=“250”;

property name=“body” ormtype=“text”;

property name=“attachment” ormtype=“string” length=“250”;

property name=“enabled” ormtype=“boolean” default=“true”;

property name=“tstamp” ormtype=“timestamp” ;

//associations

property name=“profile” fieldtype=“many-to-one” cfc=“Profile” fkcolumn=“profileid” inverse=“true” lazy=“true”;

property name=“category” fieldtype=“many-to-one” cfc=“ForumCategory” fkcolumn=“categoryid” inverse=“true” lazy=“true”;

property name=“replies” fieldtype=“one-to-many” type=“array” singularName=“reply” cascade=“delete-orphan” cfc=“ForumReply” fkcolumn=“topicid”;

//getPublishedReplies

public function getPublishedReplies(){

var replies = findAllWhere(entityName=“ForumReplies”,criteria={ topicid = this.getID(), enabled = true });

return replies;

}

}

`

Can anyone suggest how best I would go about getting the associated replies for a topic, while being able to filter on on fields in the associated entity? All I can think is that I may need to resort to Raw SQL, but surely there is a better way than that?

Many thanks for any pointers.

Jason

You could add a “publishedReplies” relationship to your ForumTopic entity that has a “where” condition which filters on the enabled value (see “Applying filter on associated objects” here http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WS5FFD2854-7F18-43ea-B383-161E007CE0D1.html)

For a real-life example, check out the BaseContent.cfc entity in the ContentBox repo: https://github.com/Ortus-Solutions/ContentBox/blob/development/modules/contentbox/model/content/BaseContent.cfc

The “activeContent” one-to-many relationship uses an approach like this.

Hope that helps!

Joel, you’r my hero! That’s perfect. Why oh why have been ignorant to that feature of entities for so long!

Thanks!!

Sure thing, glad it was helpful!