Multiple Inserts in Model

30 views
Skip to first unread message

Risto

unread,
Mar 24, 2010, 5:06:24 PM3/24/10
to ColdFusion on Wheels
Hi,

I'm using a join table and would like to know a better way to loop
through my ids
and insert into the join table in the model instead of the controller.

This is what I use in the controller and it works fine, but when I do
a beforeCreate() in the model that calls my
insert function it's not getting my 2 variable sent to that function.

So my questions:

How to pass my 2 variables from the controller to the model.
How to loop through the saves in the model.
Is there anything wrong with the way I have it working from the
controller now?

Thanks

<!--- create function in my controller --->

<cffunction name="create">

<!--- multiple select city ids from form --->
<cfset var mynewid = restaurant.id>

<!--- multiple select city ids from form --->
<cfset var idlist = params.cityid>

<!--- Loop through ids, insert into db --->
<cfloop index="i" list="#idlist#" delimiters=",">
<cfset restaurantCityBridge = model(" restaurantcitybridge").new()>
<cfset restaurantCityBridge.cityid = i>
<cfset restaurantCityBridge.restaurantid = mynewid>
<cfset restaurantCityBridget.save()>
</cfloop>
</cffunction>

Chris Peters

unread,
Mar 24, 2010, 5:57:13 PM3/24/10
to cfwh...@googlegroups.com
Are you creating a new restaurant just before adding the restaurantCityBridge data?


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


Risto

unread,
Mar 24, 2010, 7:50:47 PM3/24/10
to ColdFusion on Wheels
Hi,

Yes, and the restaurant.id is the newly inserted id of the restaurant.

Thanks

On Mar 24, 5:57 pm, Chris Peters <ch...@clearcrystalmedia.com> wrote:
> Are you creating a new restaurant just before adding the
> restaurantCityBridge data?
>

> > cfwheels+u...@googlegroups.com<cfwheels%2Bunsu...@googlegroups.com>

Per Djurner

unread,
Mar 25, 2010, 3:19:01 PM3/25/10
to cfwh...@googlegroups.com
I would create a new function in your model CFC file and just pass
everything into it.
That would keep your controller cleaner and you can call it from other
places of your code in the future without duplicating too much code.
Also think about whether you need to wrap your code in cftransaction
tags (since you are causing multiple database calls behind the scenes
it's a good idea to use transactions so that either all of them gets
executed or none).

> To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.

Risto

unread,
Mar 26, 2010, 9:43:30 AM3/26/10
to ColdFusion on Wheels
Great Point. Thanks

On Mar 25, 3:19 pm, Per Djurner <per.djur...@gmail.com> wrote:
> I would create a new function in your model CFC file and just pass
> everything into it.
> That would keep your controller cleaner and you can call it from other
> places of your code in the future without duplicating too much code.
> Also think about whether you need to wrap your code in cftransaction
> tags (since you are causing multiple database calls behind the scenes
> it's a good idea to use transactions so that either all of them gets
> executed or none).
>

Reply all
Reply to author
Forward
0 new messages