config()
method, you can save changes to that model and its associated models in a single call with save(), create(), or update().user
model that has one profile
:user
object and the profile
object in a single call to create().profile
nested within the user
object, we must create a new instance of it and set it as a property in the call to user.new()
.newProfile
object set in the first line of the action, we can var
scope it to clearly mark our intentions for its use.edit
action calling an existing object, our call would need to look similar to this:profile
property, you must include that association in the finder call with the include
argument.views/users/new.cfm
will end up looking like this:profile
model, which contain an extra argument for association
. This argument is available for all object-based form helpers. By using the association
argument, Wheels will name the form field in such a way that the properties for the profile will be nested within an object in the user
model.create
action does not change at all from what you're used to.user.save()
in the example above, Wheels takes care of the following:user
model.user
called profile with the profile
data stored in an object.profile
model.edit
scenario, this is what our update
action would look like (which is very similar to create
):user
can have many addresses
. Furthermore, we know that each user
has only one profile
. user
model, let's add an association called addresses
and also enable it as nested properties.addresses
table contains a foreign key to the Users
table called userid
, Now in the addresses
model, let's associate it with its parent User
and also enable it as nested properties.address
in the array.edit
scenario, we just need to remember to call the include
argument to include the array of addresses saved for the particular user
:views/users/_address.cfm
. Wheels will loop through each address
in your nested properties and display this piece of code for each one.position
. Without having a unique position identifier for each address
, Wheels would have no way of understanding which state
field matches with which particular address
, for example.arguments.current
for position
. This value is set automatically by Wheels for each iteration through the loop of addresses
.user
and its addresses
is exactly the same as the code demonstrated in the Saving the Object and Its Nested Properties section earlier in this chapter.customers, publications
, and subscriptions
, straight from the Associations chapter.customer
s' subscriptions in the subscriptions
join table, one approach is to loop through data submitted by checkBoxTag()s from your form, populate subscription
model objects with the data, and call save(). This approach is valid, but it is quite cumbersome. Fortunately, there is a simpler way.customer
model for this example:edit
action in the controller at controllers/Customers.cfc
.customer
with its associated subscriptions
included with the include
argument. We also need all of the publication
s in the system for the user to choose from.publications
to assign to the customer
.views/customers/edit.cfm
is where the magic happens. In this view, we will have a form for editing the customer
and check boxes for selecting the customer
's subscriptions
.<fieldset>
for Subscriptions, which loops through the query of publications
and uses the hasManyCheckBox() form helper. This helper is similar to checkBox() and checkBoxTag(), but it is specifically designed for building form data related by associations. (Note that checkBox() is primarily designed for columns in your table that store a single true/false
value, so that is the big difference.)objectName
argument passed to hasManyCheckBox() is the parent customer
object and the associations
argument contains the name of the related association. Wheels will build a form variable named in a way that the customer
object is automatically bound to the subscriptions
association.keys
argument accepts the foreign keys that should be associated together in the subscriptions
join table. Note that these keys should be listed in the order that they appear in the database table. In this example, the subscriptions
table in the database contains a composite primary key with columns called customerid
and publicationid
, in that order.update
action is fairly standard for a Wheels application:parent
customer object:customers
table with any changes submitted in the Customers <fieldset>
.subscriptions
table depending on which check boxes are selected by the user in the Subscriptions <fieldset>
.