Skip to main content
Participant
November 25, 2010
Question

How to define a Parent/Child relationship with CF9 ORM

  • November 25, 2010
  • 1 reply
  • 1177 views

Hi,

a rather simple database scheme

Object {

    id,

    name

}

ObjectRelation {

    parentID, -- points to id in the Object table

    childID    -- points to id in the Object table

}

how would i define an Object Model component with properties

component {

    property "name"

    property "children"

    property "parent"

}

obviously root object(s) don't have any parent elements.

This topic has been closed for replies.

1 reply

Owainnorth
Inspiring
November 25, 2010

Quick question - have you read *any* of the documentation? What have you tried thus far?

Participant
November 25, 2010

i actually RTFM! so far i got the children relationship working like this

property name="children"
    fieldtype="one-to-many"
    cfc="Object"
    linktable="ObjectRelation"
    fkcolumn="parentID"
    singularname="child"
    lazy=true
    inversejoincolumn="childID";

i had the "inverse" version of this for the parent relationship, it didn't work all to well though.

property name="parent"
    fieldtype="many-to-one"
    cfc="Object"
    linktable="ObjectRelation"
    fkcolumn="childID"
    lazy=true
    inversejoincolumn="parentID";

with that whenever i try to call object.getParent().getName() i get a

Error Messages: Value must be initialized before use.
Its possible that a method called on a Java object created by CreateObject returned null.

Owainnorth
Inspiring
November 25, 2010

Glad to hear it

I haven't done any ORM in a while so I'm sure someone will correct me, but I don't believe you need the linktable attribute unless it's a many-to-many; in your case you should be able to add a property to the child object called parent, with a cfctype of whatever it is, and a fkcolumn attribute of the foreign key column.

That should be all you need. Try cutting your attributes down to the absolute bear minimum and try that, alas I don't have a working example to hand I'm afraid.

O.