Search code examples
ormcoldfusioncoldfusion-9

coldfusion 9 orm one-to-one multiple cfc


I am using CF9 ORM -

I have an object model where I have multiple objects that could have a one-to-one relationship to a one particular object. Is there a way to specificy the inverse relationship to one of two potential CFCs?

CFC 1a (ProblemType1):

property name="Product" cfc="Product" fieldtype="one-to-one" fkcolumn="productID" ;

CFC 1b (ProblemType2):

property name="Product" cfc="Product" fieldtype="one-to-one" fkcolumn="productID" ;

CFC 2:

property name="Problem" fieldtype="one-to-one" cfc=???;

Can I use an interface for this? Or...?


Solution

  • CFC 1a and CFC 1b can both be subclasses of a parent entity, CFC 1. CFC 1 should have the relationship to "Product," which will be inherited by both of the subclasses. CFC 2 can then point to CFC 1 in its relationship.

    Sample entities:

    /** CFC 1 **/
    component persistent="true" {
    
        property name="Product" cfc="Product" fieldtype="one-to-one" fkcolumn="productID";
    
    }
    
    /** CFC 1a **/
    component persistent="true" extends="baseProblem" {
    
        // problemtype1 specific properties go here
    
    }
    
    /** CFC 1b **/
    component persistent="true" extends="baseProblem" {
    
        // problemtype2 specific properties go here
    
    }
    
    /** CFC 2 **/
    component persistent="true" {
    
        property name="Problem" fieldtype="one-to-one" cfc="baseProblem";
    
    }
    

    If you do take this approach, you'll probably need to look into inheritance mapping, specifically the discriminatorColumn and discriminatorValue attributes. Without knowing how your DB schema is set up, it's difficult to give further advice on this point, but the documentation should get you started.