ColdFusion/ORM Example - Filtering with a many to many

Let me be clear - I’m not sure this is the best way to do this - but it worked and as it took me a while to Google up the answer and test it, I thought I’d share.

Ok - so imagine an entity that has a many-to-many relationship. A good example is a blog entry with many categories. Any one blog entry may have multiple categories assigned to it. How would you write a query to return all blog entries in a specific category? Here is how I did it (and I apologize for not linking to the post - I forget exactly what I saw this):

<cfset r = ormExecuteQuery("from blogentry as be left join be.categories as cat where cat.id=4")>

Obviously the 4 there is hard coded and would be replaced with a bound parameter.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments