2

I'm an old CFML developer, new to CF on Wheels and MVC programming in general. I'm picking it up pretty quickly, but one thing that isn't evident to me is how one can offer a form to optionally update multiple db table records (models). I'd specifically like to set up a tabbed form for User info and User Profile info, where the former is required and the latter is not. This data is stored in two different one-to-one tables. What's the setup I need in order to call two "new" or "edit" views, run 2 "create" or "update" procedures, affecting two different tables. Or am I thinking about this all wrong.

Update: Adding some more info on what I'm trying to do. To keep it simple, I'll stick to 2 tabs and 2 tables, though I'm really looking at at least 3 in this instance.

So I've got a Users table and a UserProfiles table, and I've got models named User.cfc and UserProfile.cfc that are related 1-to-1, with UserProfile dependent on User. Pretty standard stuff. For each I've got controllers: Users.cfc and UserProfiles.cfc, each of those containing actions. add, edit, create, update, doing the obvious stuff (add and edit display forms). I have partials that display the add/edit form fields for each, so that's already prepared. Now, I want to create what is effectively a single add/edit form that can update both tables at the same time. The tabs don't really matter; effectively it could all be on one page.

So conceptually I'm doing something like:

#startFormTag(action=???)#
#includePartial("form_user_add-edit")#
#includePartial("form_userprofile_add-edit")#
<button type="submit" class="btn">#operation#</button>
#endFormTag()#

Do I need to create a separate controller action that basically combines the create and update actions for two different controllers?

Thanks in advance from a pleased and eager CFWheels newbie...

Brian

Brian D
  • 833
  • 7
  • 11

2 Answers2

1

If all of the data is related through hasMany or hasOne associations, I'd recommend looking at nested properties.

http://cfwheels.org/docs/1-1/chapter/nested-properties

If you're a newbie though, you may want to refrain from this until you've got something simpler worked out.

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
  • Thanks Chris. I have managed to pull this off the imperfect way, and now I'm going back through and using nested properties. The main hurdle for me was conceptual: it seems that in MVC, it's one-view, one-controller, but possibly multiple models addressed by one controller. So as long as my controllers and views line up, I can use them to interact with and display multiple models. Cool! – Brian D Jun 16 '12 at 21:22
0

I guess you are talking about two models representing these two tables, possibly associated using hasOne. Models allow you to validate data, this makes controller much simpler. This way you could create two forms under two tabs, and keep record's primary key as hidden field. Controller could run the validation and re-display the forms (partials may help)... Hold on, I am just going through the reference.

I realize this answer is pretty generic, as well as your question. I suggest you to go ahead and try something, see how it works.

After that update your question with code samples and ask if you have some specific problems. For example, validation and displaying errors in CFWheels may be a bit tricky.

Sergey Galashyn
  • 6,946
  • 2
  • 19
  • 39
  • Thanks for the input, Sergii. It's certainly fair enough to say I'm being too vague. I have been trying several approaches, but before I can even run any code, I keep running into obvious blocks. Let me see if I can be more specific. I a Users table and a UserProfiles table and a UserSettings table, all one-to-one. Every user has a record in each. This isn't absolutely necessary to be broken down, but for a few reasons I'd really prefer it. – Brian D Jun 13 '12 at 20:35
  • What I'm trying to do is enable any of those forms to have a separate controller and a separate view and a separate model, but also to be all done -- at least for the Update action -- all at once. So the user clicks the "Main" tab or the "Profile" tab or the "Settings" tab and can change their info in each, but "Save" it all at once, so I guess I'd then need to call all 3 save() actions in one place. But I can't figure out a structure or a sequence. And yes I'm already using partials for the forms, but the hard part is how to conditionally or simultaneously submit to all 3 update actions... – Brian D Jun 13 '12 at 20:38