Caching of new objects that aren't persisted

0 views
Skip to first unread message

James Allen

unread,
May 5, 2008, 9:23:01 AM5/5/08
to transfer-dev
Hi guys,

Quick question that probably has an easy answer but..

When I am creating a new object in Transfer, I transfer.new() it, fill
it with data, validate and then save.

However, if validation fails the new object is kept in the transfer
cache. Therefore, the item appears on the website before it's been
saved.

What is the correct way to create a new object and only have it appear
once it's been persisted?

I was thinking maybe transfer.new() and then cloning the object. Would
that be the right way to do it?

Thanks in advance,
James.

Mark Mandel

unread,
May 5, 2008, 9:50:08 AM5/5/08
to transf...@googlegroups.com
James,

New objects aren't cached... so.. I'm not sure what you are driving at here?

Unless your 'save' is running, regardless of if your validate() has failed?

Mark

--
E: mark....@gmail.com
W: www.compoundtheory.com

James Allen

unread,
May 5, 2008, 10:17:41 AM5/5/08
to transf...@googlegroups.com
Hi Mark,

No I checked the DB and nothing is saved. Is this happening because I am
attaching the object to it's parent. I.E this is a new answer being created
which has a setParentQuestion() method run to tie it to an already persisted
question. Therefore the answer appears immediately even if I don't save it.
Once I re-init transfer it goes. Here's the creation code:

<cfargument name="ID" type="numeric" default="0" />
<cfargument name="AnswerText" type="string" required="false"
/>
<cfargument name="AnswerOption" type="numeric"
required="false" />
<cfargument name="Rating" type="numeric" required="false" />
<cfargument name="PosterIP" type="string" required="false"
/>
<cfargument name="PostDate" type="date" required="false" />
<cfargument name="userBean" type="component"
required="false" />
<cfargument name="questionBean" type="question"
required="false" />

<cfset var answerBean =
variables.instance.transfer.new("question.Answer") />

<cfif len(AnswerText)><cfset
answerBean.setAnswerText(answerText) /></cfif>
<cfif len(AnswerOption)><cfset
answerBean.setAnswerOption(answerOption) /></cfif>
<cfif len(Rating)><cfset answerBean.setRating(rating)
/></cfif>
<cfif len(PosterIP)><cfset answerBean.setPosterIP(PosterIP)
/></cfif>
<cfif len(PostDate)><cfset answerBean.setPostDate(PostDate)
/></cfif>

<cfif StructKeyExists(arguments,"questionBean") AND
isObject(questionBean)>
<cfset answerBean.setParentQuestion(questionBean)>
</cfif>

<cfif StructKeyExists(arguments,"userBean") AND
isObject(userBean)>
<cfset answerBean.setUser(userBean)>
</cfif>

<cfreturn answerBean />

Cheers,
James.

Mark Mandel

unread,
May 5, 2008, 12:16:47 PM5/5/08
to transf...@googlegroups.com
Why not just set the parent if the validation passes?

Failing that, you should be able to clone the parent, and then save it that way.

Mark

James Allen

unread,
May 5, 2008, 12:46:28 PM5/5/08
to transf...@googlegroups.com
Aha I see. Yeah I did think cloning might be the answer. I can't really do
the parent attaching at save because the save is carried out by the
decorator.

The parent cloning idea might work a treat though - thanks.

I'll go this route in future then if it's the recommended approach.

Reply all
Reply to author
Forward
0 new messages