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....