I already had this question once, but slack isn’t showing me the result.
I have a many-to-one relation from table a to b
How can I get the vice versa reference to the related record from b to a??
one-to-many
is the vice versa. So:
article.cfc
property name="category" relationship="many-to-one" relatedto="category";
property name="topic" relationship="many-to-one" relatedto="topic";
// ...
category.cfc
property name="articles" relationship="one-to-many" relatedto="article" relationshipkey="category";
Example query utilisation
selectData( objectName="category", filter={ "articles.topic"=topic } );
selectData(
objectName = "category"
, selectFields = [ "category.label as cat", "articles.id", "articles.label", "articles$topic.label as topic" ]
, filter = { id=categoryId }
);
https://docs.preside.org/devguides/dataobjects.html#one-to-many-relationships
one-to-many relationships are also commonly used for formula fields such as number_articles
. From Preside 10.23 there are also aggregate helpers that make this easy and more query efficient:
property name="articles" relationship="one-to-many" relatedto="article" relationshipkey="category";
property name="article_count" formula="agg:count{ articles.id }" type="numeric";
https://docs.preside.org/devguides/dataobjects.html#aggregate-functions-in-formula-fields
1 Like