how to you update with out selecting first (and how does wheels know when to do an update versus insert)?

19 views
Skip to first unread message

Jacob

unread,
Feb 3, 2012, 1:25:19 AM2/3/12
to ColdFusion on Wheels
First off, this is my first post, and I've only been working with
wheels for a little more than a week and loving it. Great great work,
wheels guys.

Anyway, questions like this have been touched on before (eg:
http://groups.google.com/group/cfwheels/browse_thread/thread/2857bbe77867ab6e/57fc90b83b95f0b9?lnk=gst&q=save#57fc90b83b95f0b9),
but I'm still a little confused.

If you have to fetch a record in order to update it, doesn't that add
an extra database call when handling form submissions? I'm pretty much
a self-taught hack, so there may be some good reasons for this, but
I'd like to know what they are.

Before diving into wheels, here's how I'd usually handle form posts:
<!---existence of PK ID in post indicates it's an update--->
<cfif len(form.id)>
1- [create instance in memory and assign prop values based on
posted data]
2- [pass instance to DAO; it will know to perform update due to
the existence of ID]
<cfelse>
[create instance in memory and assign prop values based on posted
data]
[pass instance to DAO; it will know to perform insert since
there's no ID]
</cfif>

In wheels, as far as I can tell, there's an extra DB call in the
update process

<cfif len(parms.id)>
1- [get record by id from db, pass to instance]
2- [create instance in memory and assign prop values based on
posted data]
3- [pass instance to model-- it knows to UPDATE based on how the
instance was created]
<etc...>

Is there a way to skip step 1? Is there a good reason for it?
Security? I tried using updateByKey() with instantiate=false but it
didn't work:
<cfset return = model("test").updateByKey(key=33,test="go
wheelzzz!",instantiate=false)> (doesn't do an update at all, for some
reason...)

Lastly, as sort of a corollary, how does the model 'know' how the
instance was created in step 3?
EG: this code kinda threw me for a loop

<!---I would expect this to do an update on the db record with ID =1
but it updates the row with the original Id (33). Where is the
reference to the original id in the object? When I dump test there,
the ID prop is set to 1--->
<cfset test = model("test").findByKey(33)>
<cfset test.setProperties({id=1,test="hola moonde"})>
<cfdump var="#test#">
<cfset test.save()>

Thanks again. Hope to start contributing at some point....

Per Djurner

unread,
Feb 8, 2012, 6:26:38 PM2/8/12
to cfwh...@googlegroups.com
How Wheels knows if it's new or not:
If you call new(), it's new :)
(It has nothing to do with the primary key existing or not.)

Yes, that extra database call is "by design" so to speak.
The updateByKey method is one way around it (not recommended though unless you have serious database performance issues).

/ Per


--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.


Jacob

unread,
Feb 8, 2012, 8:31:17 PM2/8/12
to ColdFusion on Wheels
Thanks, Per.

I'll take your word for it that the extra DB call is by design, tho I
have a knee jerk instinct against any extra trips to the DB. Must be
from all the FoxPro in my youth.

I was digging just now and found the $persistedProperties struct that
stores object props when first created (from the db). So, when
model.save() is called, it will first check if the instance props have
changed at all from the $persistedProps, and if they have, build the
update statement with the new prop values EXCEPT the PK fields? which
will always take the values from the $persistedProps struct? Clearly,
a gross generalization, (perhaps both meanings of 'gross' :) ), but is
that the basic idea?

Thanks again. Really kick ass framework.



On Feb 8, 3:26 pm, Per Djurner <per.djur...@gmail.com> wrote:
> How Wheels knows if it's new or not:
> If you call new(), it's new :)
> (It has nothing to do with the primary key existing or not.)
>
> Yes, that extra database call is "by design" so to speak.
> The updateByKey method is one way around it (not recommended though unless
> you have serious database performance issues).
>
> / Per
>
>
>
>
>
>
>
> On Fri, Feb 3, 2012 at 7:25 AM, Jacob <jacobbd...@yahoo.com> wrote:
> > First off, this is my first post, and I've only been working with
> > wheels for a little more than a week and loving it. Great great work,
> > wheels guys.
>
> > Anyway, questions like this have been touched on before (eg:
>
> >http://groups.google.com/group/cfwheels/browse_thread/thread/2857bbe7...
> > ),
Reply all
Reply to author
Forward
0 new messages