Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion ORM Relationship

Tags:

coldfusion

orm

Before ORM, if I wanted to display the combined output of a normalized table, I’d just do a quick CFQUERY, join the tables on the fields I want and display the output. I just can’t get my head wrapped around it using ORM.

For example with these two tables:

customers
(id,
 name,
 customerType)

customerTypes
(id,
Name)

How would you create a single entity you can load to display the following when the customerType field in customers links to an id in customerTypes?

customers.id, customers.name, customerTypes.name

All of the ORM relationship examples I’ve walked through for some reason can’t make me understand how to do it. It seems so simple it’s killing me. Any help shedding some light on this would be appreciated!

like image 539
Robb Hartzog Avatar asked Nov 05 '22 02:11

Robb Hartzog


2 Answers

Or alternatively

<cfproperty name="type" type="string" column="Name" table="customerTypes" joincolumn="id"> 

see Join mapping in a CFC

like image 82
Henry Avatar answered Nov 09 '22 07:11

Henry


So in your Customers CFC you will need something like this:

<cfproperty name="customerType" type="CustomerTypes" fieldtype="many-to-one" cfc="CustomerTypes" fkcolumn="id" lazy="true" />

Then you should be able to dump an instance of a Customers object and see that it has a customerType property and hence you can write something like this:

<cfset cust = entityLoad("Customers", 1) />
<cfset type = cust.getCustomerType().getName() />

Hope that helps!

like image 40
Ciaran Archer Avatar answered Nov 09 '22 07:11

Ciaran Archer