Skip to main content
ashar2005
Participant
October 7, 2009
Question

ORM - Join table to get lookup values

  • October 7, 2009
  • 1 reply
  • 1181 views

I know you can join one cfc entity to another cfc entity, I’m not trying to do that.

I am trying to populate a column added to an entity (not persistent) with a string from a joined table’s column.  I’ve read everything about joincolumn, mappedby, elementColumn., and tried different combinations in order to put the correct parameters in the cfproperty tag but there doesn’t seem to be a solution for this simple thing.

Using an Address example.... instead of storing the state's name
in the address table, you would store the value state_id.  And for this example’s purpose you would have a lookup table called states

that only has two columns, the Primary key named id, and the label column called state_name.

TABLES:

States  (lookup table)
id - int  (PK)
state_name - string

Address table

street_number - string

street_name - string

state_id - int (FK)

zip_code - string

I would like to put in an additional field in Address entity called stateName
that will use its own state_id column value to join to the id column of the

States table and retrieve the value of the state_name field.

I am currently accomplishing this in the entities by using formula fields....
<cfproperty name="streetName" persistent = "false" formula = "select b.state_name from States b where b.id = state_id" />
and it works fine.

Every join example I run across has you create a complete entity for the joined table and join the cfc which seems like overkill since the lookup tables should not be updated with the entity and I do not want to have to make an enitty of something (lookup table) with just one column that is needed,

and then instead of address.getStateName I would have to do  address.getState().getStateName()

I have some entities that will have several of these joined lookup fields.
I just thought there would be a better way to simply define the join table, join fields in order
to do a simple join field so that you don’t have SQL code in entities that use lookup tables.

TIA

Ashar

    This topic has been closed for replies.

    1 reply

    October 7, 2009

    Without seeing your code I can not work out exactly what you are trying to achieve...

    If I understood it correctly, then the solution is simple (at least it looks like it is simple to me):

    Do NOT specify a StateName field in your Address object at all, but define getStateName() method to call (static) method of States object...

    If your WhatEver table is NOT updateable (static) you create static object (no properties, only methods) and use static methods...

    ashar2005
    ashar2005Author
    Participant
    October 7, 2009

    Thanks for the response, but most examples that I've seen including the

    Adobe Artists/Art example in the help docs show the additional column

    being added to the entity.  I'm looking for a similar solution by just using the attributes

    of the property tag to do a join, and only retrieving the joined table column value.

    As I mentioned before, I have the additional stateName property added and

    am using the "formula" setting with a sql command to do the join, and it works fine

    since the States table should not be updated.  See working entity below...

    I'm just looking for a way to use different attributes of the stateName property,

    i.e. jointable, mappedBy, elementColumn, etc, instead of the "formula" attribute

    to achieve the same thing so the entities won't have sql code in them.

    Current Address entity:

    component
        name        = "Address"
        displayname = "Address"
        output      = "false"
        persistent    = "true"
        table        = "address"
        accessor    = "true"
    {
        property name="Id"                       column="id"                    type="numeric"     fieldtype="id"    generator="identity" ;

        property name="streetNumber"      column="street_number"   type="string"      default = ""  ;

        property name="streetName"         column="street_name"     type="string"      default = ""  ;

        property name="stateName"        persistent="false"            type="string"    default = ""

            formula="select b.state_name from States b where b.id = state_id" ;

    For that property I would rather the property defined with attributes similar to :

    property name="stateName"

           fkcolumn= "state_id"

           jointable = "states"

           mappedBy = "id"

           elementColumn = "state_name"

           persistent = "false"

           type = "string"

           default = "" ;

    } // Address.cfc