saving related objects correctly

30 views
Skip to first unread message

Jonathan Price

unread,
Mar 27, 2015, 7:09:01 PM3/27/15
to cf-or...@googlegroups.com
I'm sure this is trivial (and I'm embarrassed to be asking because I'm sure it belies some larger problems), but what's the best way to handle something like this. Consider the related objects:

<cfcomponent persistent="true" table="WebRequest">
<cfproperty name="id" column="WebRequestID" generator="increment">
</cfcomponent>

<cfcomponent persistent="true" table="WebRequestAlert">
<cfproperty name="id" column = "WebRequestAlertID" generator="increment">
<cfproperty
name="WebRequest"
fieldtype="many-to-one"
cfc="WebRequest"
fkcolumn="WebRequestID">
</cfcomponent>

The each have other fields, but for simplicity's sake...

So, I have a service that will first create the WebRequest in the database using EntityNew and EntitySave. After that, I feed the WebRequest to the WebRequestAlert to create the relationship. My thought was that the WebRequest needs to be persisted before the WebRequestAlert can receive the WebRequest, so I wound up with something like this:

<cfset LOCAL.o_WebRequest = EntityNew("WebRequest")>
<cfset LOCAL.o_Alert = EntityNew("WebRequestAlert")>

<cfset EntitySave(LOCAL.o_WebRequest)>
<cfset ormFlush()>
<cfset LOCAL.o_Alert.setWebRequest(LOCAL.o_WebRequest)>
<cfset EntitySave(LOCAL.o_Alert)>


Is the ormFlush necessary? And what's the best practice for this?


Thanks,
Jonathan




Cameron Childress

unread,
Mar 28, 2015, 10:32:22 AM3/28/15
to cf-or...@googlegroups.com
On Fri, Mar 27, 2015 at 7:09 PM, Jonathan Price wrote:
Is the ormFlush necessary?  And what's the best practice for this?

I don't think it's required but that easy enough to test. Remove it and run the code.

If you haven't already gotten a copy I highly recommend John Wish's book on ORM. It covers so much and is super handy when first getting into ORM:


The book is a few years old now but most (all?) the information is still very relevant.

-Cameron
 
--
Cameron Childress
--
p:   678.637.5072
im: cameroncf

Puritan Paul

unread,
Mar 28, 2015, 8:52:40 PM3/28/15
to cf-or...@googlegroups.com
It’s weird.  We’ve started seeing some "Duplicate Entry for key PRIMARY” errors that looks to revolve around a couple of EntitySave commands.  We’re not currently using transactions - just been letting the end of request do the flushing for us - and I don’t get the feeling that that’s best common practice.

We’ve temporarily taken out the section of code that appears to be the problem, but I’m curious how you folks tend to implement your transactions?  I’m guessing that most entry-point type service calls are just encapsulated with the transaction tag?  I’ll do some poking around, but all suggestions/leads welcomed. 

Thanks, and thanks for the book tip.



--
You received this message because you are subscribed to the Google Groups "cf-orm-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cf-orm-dev+...@googlegroups.com.
To post to this group, send email to cf-or...@googlegroups.com.
Visit this group at http://groups.google.com/group/cf-orm-dev.
For more options, visit https://groups.google.com/d/optout.

Puritan Paul

unread,
Mar 28, 2015, 9:51:50 PM3/28/15
to cf-or...@googlegroups.com
Started a new topic with broader coverage and a little more description, FYI.  Thanks again.


On Mar 28, 2015, at 7:32 AM, Cameron Childress <came...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages