Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:06:31+00:00 2026-05-29T05:06:31+00:00

First, the relevant bits of the table structure: contact -contactID -email data -value -contactID

  • 0

First, the relevant bits of the table structure:

contact
    -contactID
    -email

data
    -value
    -contactID
    -definitionID

definition
    -definitionID
    -name

Each record in the contact table represents a single contact along with their e-mail address.

Each record in the definition table represents the definition of a custom data field. For example, there might be five records in definition – Organization, Zip Code, Comments, Address, Phone. In addition to the names of the fields, it also defines relevant meta-data about the fields.

Each record in the data table contains the value of the custom data field as it relates to the contact. Its definition is taken from the definition table.

To further clarify, if I wanted to generate a table of contacts along with their custom field data, it might look like this:

| E-Mail            | Organization | Zip Code | Comments                         | Address       | Phone    |
-------------------------------------------------------------------------------------------------------------
| sean@example.com  | ACME         | 12345    | Cool guy!                        | 123 Test St   | 555-5555 |
| sean2@example.com | SomeCo.      | 54321    | Doesn't know anything about ORM! | 321 Test Blvd | 444-4444 |

The benefit of this system is that it can scale out as much as I need it to and is easily customizable. The detriment is that I have no idea how to define the relationships 🙂

I’ve attempted defining contact:data as 1:M and definition:data as 1:M, but the results seem a little odd: With two contacts and a single definition, adding a row to data for each contact and then calling entityLoad( 'Contact' ) results in an interesting relationship. It appears like this(just going to use some pseudo-struct notation because it’s easy enough to type and, I hope, read):

{
    contact: {
        email: 'sean@example.com',
        data: {
            value: 'ACME',
            definition: {
                name: 'Organization',
                data: {
                    value: 'SomeCo.',
                    contact: {
                        email: 'sean2@example.com'
                }
            }
        }
    }
}

It looks like it is creating an indirect relationship between contact and definition based on the data table’s relation to both tables. As you can imagine, upping the amount of contacts and custom fields just makes the problem exponentially worse.

Is this type of relationship possible using CF9’s ORM? How can I accomplish it?

Thanks in advance!

EDIT: Forgot to specify – I am using MySQL, if it matters.

EDIT 2: CFC definitions follow:

Contact.cfc

/**
 * @persistent true
 */
component name='Contact' {

    /**
     * @type numeric
     * @sqltype int(11)
     * @generator increment
     * @fieldtype id
     */
    property contactID;

    /**
     * @type string
     * @sqltype varchar(50)
     */
    property email;

    /**
     * @type array
     * @fieldtype one-to-many
     * @cfc Data
     * @fkcolumn dataID
     */
    property data;
}

Definition.cfc

/**
 * @persistent true
 */
component name='Definition' {

    /**
     * @type numeric
     * @sqltype int(11)
     * @generator increment
     * @fieldtype id
     */
    property definitionID;

    /**
     * @type string
     * @sqltype varchar(50)
     */
    property name;

    /**
     * @type array
     * @fieldtype one-to-many
     * @cfc Data
     * @fkcolumn dataID
     */
    property data;

}

Data.cfc

    /**
 * @persistent true
 */
component {

    /**
     * @type numeric
     * @sqltype int(11)
     * @generator increment
     * @fieldtype id
     */
    property dataID;

    /**
     * @type string
     * @sqltype varchar(50)
     */
    property value;

    /**
     * @fieldtype many-to-one
     * @fkcolumn contactID
     * @cfc Contact
     * @inverse true
     */
    property contact;

    /**
     * @fieldtype many-to-one
     * @fkcolumn definitionID
     * @cfc Definition
     * @inverse true
     */
    property definition;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:06:32+00:00Added an answer on May 29, 2026 at 5:06 am

    I believe you want the relationships to be handled by Data.cfc, having

    Data --m2o--> Contact
    Data --m2o--> Definition
    

    and for convenience

    Contact --o2m--> Data (inverse=true)
    

    CFC’s

    // Contact.cfc
    component persistent="true" {
      property name="contactID" fieldtype="id" generator="native";
      property name="email";
      property name="data" cfc="Data" fieldtype="one-to-many" inverse="true" lazy="true";
    }
    
    // Definition.cfc
    component persistent="true" {
      property name="definitionID" fieldtype="id" generator="native";
      property name="name";
    }
    
    // Data.cfc
    component persistent='true'{
      property name="dataID" fieldtype="id" generator="native";
      property name="value";
      property name="contact" cfc="Contact" fieldtype="many-to-one" fkcolumn="contactID";
      property name="definition" cfc="Definition" fieldtype="many-to-one" fkcolumn="definitionID";
    }
    

    index.cfm

    <cfscript>
    ormReload();
    transaction {
        con = new Contact();
        con.setEmail("chris@domain.com");
        entitySave(con);
    
        def1 = new Definition();
        def1.setName("twitter");
        entitySave(def1);
    
        def2 = new Definition();
        def2.setName("interests");
        entitySave(def2);
    
        data1 = new Data();
        data1.setValue("d1rtym0nk3y");
        data1.setDefinition(def1);
        data1.setContact(con);
        entitySave(data1);
    
        data2 = new Data();
        data2.setValue("ColdFusion");
        data2.setDefinition(def2);
        data2.setContact(con);
        entitySave(data2);
    
        // this is important, you must set both sides of the relationship or "bad things" happen
        // i'd recommend overriding contact.addData()/data.setContact to ensure both sides get set
        con.addData(data1);
        con.addData(data2);
    }
    writeDump(con);
    </cfscript>
    

    To retrieve the Data attributes for a Contact in a sensible fashion you can do

    myData = ormExecuteQuery("
        select new map(
            data.value as value,
            def.name as name
        )
        from Data data
        join data.definition def
        where data.contact.contactID = :id
    
    ", {id=con.getContactID()});
    
    writeDump(myData);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I need some complex algorithm I first check if there's anything relevant already
What's the best way in Ruby (with Rails, if relevant) to capitalize the first
First off, I read all of the suggested questions that sounded halfway relevant and
For convenience I added the relevant manpages below. My (mis)understanding first: If I need
I have a solution with two relevant projects. The first builds My.exe , and
first post here and hopefully relevant to many people. I'm working on writing integration
First the error: error: a value of type float * cannot be used to
Hi everyone I have a few questions about the django admin. First the relevant
This is nested about 10 functions deep, so I'll just paste the relevant bits:
First, the relevant xkcd comic: http://xkcd.com/979/ Next, the 10-year old thread on PerlMonks: http://www.perlmonks.org/?node_id=210422

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.