Home > Developing Using CodeFluent Entities > Many-To-Many Relations in CFQL

Many-To-Many Relations in CFQL


CFQL actually provides built-in support for Many-To-Many relations, which often seems unnatural to developers. As a consequence, a common mistake is to write raw SQL to access a value contained in a Many-To-Many relation property, when in fact it can be done without any extra work.

For instance, in the following example a User can have several Roles, and one Role can be assigned to several Users. Nevertheless, if you want to load all users with a role name containing a specific token, instead of writing a raw SQL query, you could do it in a single line CFQL query, such as:

<User>
<Id/>
<Email typeName="Email" collectionKey="true"/>
<Password />
<Roles typeName="RoleCollection"/>
<cf:method name="LoadByRoleToken" body="load(string token) where Roles.Name contains @token" />
</User>

<Role>
<Id />
<Name collectionKey="true" />
<Users typeName="UserCollection" />
</Role>

As you can see in the example above, you can access entity properties using dots even on collection properties 🙂

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s