Skip to main content
March 22, 2012
Question

ORM question

  • March 22, 2012
  • 1 reply
  • 631 views

Hello,

I am very new to ORM and wanted to know how i can do the following and display one record without looping an array:

users.cfc

==============================================================

<cfcomponent persistent="true" table="tbl_users">

    <cfproperty name="uid" fieldType="id" generator="increment">

    <cfproperty name="firstname" ormtype="string">

    <cfproperty name="lastname" ormtype="string">

    <cfproperty name="username" ormtype="string">

</cfcomponent>

test.cfm

==============================================================

<cfset u = EntityLoad("users", {username = 'test'})>

<cfoutput>

Full Name - #director.getLastName()#, #director.getFirstName()#<br/>

</cfoutput>

when i run this it displays a blank page, but i dump the variable with cfdump i can see it has the right information. Also i loop through it as an array it will also display the results, but since its only one record, is there a way display the result without looping through an array.

    This topic has been closed for replies.

    1 reply

    Inspiring
    March 22, 2012

    Well don't loop.  If there's only one entry, just access it via index like you would any other array element (eg: myArray[1]).  It's not a Special ORM Array, it's just an array.

    --

    Adam

    March 22, 2012

    Thank your for the explanation, but there isn't way to use the built in function of GetFieldname like ORM has built in.

    So i can just do something #getfirstname()#?

    Inspiring
    March 23, 2012

    When you use entityLoad(), you are asking for an array of objects, so you need to access the objects at each element of the array via array notation.

    myObj = myArray[1];  // for the first element

    myObj.myGetMethod();

    or just myArray[1].myGetMethod();

    How do you expect CF to guess which element of the array you mean if you don't tell it?

    If you are certain that the filter used in entityLoad() will return one record - basically if yuo're filtering on a primary key - then you can specify unique=true and entityLoad() will return just the object.

    This is all in the docs:

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WS32C28934-CDCE-497f-8212-6342141C5846.html

    --

    Adam