views:

84

answers:

1

Hi folks,

I'm saving contacts (email, mobile phone, ICQ, AIM etc.) for people like this:

class Person {
  static hasMany = {
    contacts: Contact
  }
}
class Contact {
  String code
  ContactType type
}
class ContactType {
  String name
}

In my view, I've written some Templates for displaying each contact with a select-box for the contact-type and a textfield for the code, spiced up with some JavaScript for adding and deleting.

My question is: Is there an easy and elegant way to update the data similar to personInstance.properties = params or do I have to read out all the fields, deleting removed, updating changed and adding new ones?

A: 

I was looking into this some time ago but never got to refactor our code which handles parameters the old-fashioned way.

According to http://www.grails.org/Controllers+-+Data+Binding you can do something like this

def person = new Person(params['person'])
def contact = new Contact(params['contact'])
def conctactType = new ContactType(params['contactType'])

as long as request params are properly namespaced

person.contact.code
person.contact.type.name

You would still have to find out how to handle one to many. Maybe someone who knows can chip in.

Edit:

Came across this doc which describes how to handle one-to-many. It doesn't appear on the main grails site:

http://svn.codehaus.org/grails/tags/GRAILS_DOCS_1_1/src/guide/6.1.6%20Data%20Binding.gdoc

armandino
Thanks, that is what I was looking for.The current documentation for this can be found here:http://www.grails.org/doc/1.2.x/guide/6.%20The%20Web%20Layer.html#6.1.6%20Data%20Binding
Jan