2
votes

In our cms we have a table for tracking all objects. It accepts record id's from different objects (images, videos, posts, articles, etc. etc.) and maps them against their object type.

We are upgrading a lot of our system to using ORM and thus I need to now map the relationship from objects to their object record. (in example below I need to map an object property to the image component)

Object:
ID,
recordID,
classID

image:
ID,
...

so

object.recordID = image.ID where classID = 'image'

I'm not sure how to set this one-to-one relationship up as it is not primary key to primary key. And it is also not a unique foreign key association since its a multicolumn uniqueness constraint.

Is this not something that coldfusion can handle at this time?

1
I think a "discriminator" might be what to use for this, see help.adobe.com/en_US/ColdFusion/9.0/Developing/… - Peter Boughton
I've had a play around with that and have managed to extend the object class to an imageObject using a discriminator. but that messes up all the objects relationships because they are all still connecting to the main object using the main ID which doesn't require a discriminator, hence the relationships are crying out in pain at the loss of a discriminator... if that makes any sense whatsoever. - Rumpleteaser

1 Answers

0
votes

To avoid using the main object ID as the key, try connecting the Image object to the ImageObject sub-class you've added with a bi-directional relationship using the mappedBy attribute in Image.cfc as follows:

Object.cfc (Parent)

<cfcomponent name="Object" persistent="true" accessors="true" table="objects" discriminatorColumn="classID">

    <cfproperty name="id" fieldtype="id" generator="identity" column="ID">

</cfcomponent>

ImageObject.cfc (Child)

<cfcomponent name="ImageObject" persistent="true" accessors="true" extends="Object" table="objects" discriminatorValue="image">
    <!--- Connect to the image using the foreign key --->
    <cfproperty name="image" fieldtype="one-to-one" cfc="Image" fkcolumn="recordID">

</cfcomponent>

Image.cfc

<cfcomponent name="Image" persistent="true" accessors="true" table="images">

    <cfproperty name="id" fieldtype="id" generator="identity" column="ID">
    <!--- Connect to the ImageObject using the relationship already defined in ImageObject--->
    <cfproperty name="objectRecord" fieldtype="one-to-one" cfc="ImageObject" mappedby="image">

</cfcomponent>