Gotcha with Reactor records and exists()

Submitted by Falken on

Today I wanted to check if a particular record existed, given the primary key of that record and one additional field of it, and naively wrote:

<cfset rec=createRecord().load(id=arguments.id,otherField=arguments.otherField)>
<cfif rec.exists()>
    <cfreturn rec._getTo() >
</cfif>

The gotcha is that exists() will return true if the primary key is valid. The fact that the object has some other fields set does not matter.
In fact the transfer object that gets returned will have the 'real' value of 'otherField' over written by the value given in the load() method, which is even odder.

The work around is to load the record and then check the field:

<cfset rec=createRecord().load(id=arguments.id)>
<cfif rec.getOtherField eq arguments.otherField>
    <cfreturn rec._getTo() >
</cfif>
Sections