Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling parameters from dynamic form for one-to-many relationships in grails
    primarykey
    data
    text
    <p>My main question here is dealing with the pramas map when having a one-to-many relationship managed within one dynamic form, as well as best practices for dealing with one-to-many when editing/updating a domain object through the dynamic form. The inputs for my questions are as follows.</p> <p>I have managed to hack away a form that allows me to create the domain objects shown below in one Dynamic form, since there is no point in having a separate form for creating phone numbers and then assigning them to a contact, it makes sense to just create everything in one form in my application. I managed to implement something similar to what I have asked in my <a href="https://stackoverflow.com/questions/3329267/processing-dynamic-forms-on-the-server-side">Previous Question</a> (thanks for the people who helped out)</p> <pre><code>class Contact{ String firstName String lastName // .... // some other properties // ... static hasMany = [phones:Phone] static mapping = { phones sort:"index", cascade: "all-delete-orphan" } } class Phone{ int index String number String type Contact contact static belongsTo = [contact:Contact] } </code></pre> <p>I basically managed to get the values from the 'params' map and parse them on my own and create the domain object and association manually. I.e. i did not use the same logic that is used in the default scaffolding, i.e.</p> <pre><code>Contact c = new Contact(params) </code></pre> <p>etc...., i just looped through all the params and hand crafted my domain objects and saved them and everything works out fine. </p> <p>My controller has code blocks that look like this (this is stripped down, just to show a point)</p> <pre><code>//create the contact by handpicking params values def cntct = new Contact() cntct.firstName = params.firstName cntct.lastName = params.lastName //etc... //get array of values for number,type def numbers = params['phone.number'] def types = params['phone.type'] //loop through one of the arrays and create the phones numbers.eachWithIndex(){ num, i -&gt; //create the phone domain object from def phone = new Phone() phone.number = num phone.type = types[i] phone.index = i cntct.addToPhones(phone) } //save </code></pre> <p>My questions are as follows:</p> <ul> <li>What is the best practice of handeling such a situation, would using Command objects work in this case, if yes where can i found more info about this, all the examples I have found during my search deal with one-to-one relationships, I couldn't find an example for one-to-many?</li> <li>What is the best way to deal with the relatiohsips of the phones in this case, in terms of add/removing phones when editing the contact object. I mean the creation logic is simple since I have to always create new phones on save, but when dealing with updating a contact, the user might have removed a phone and/or editing an exiting one and/or added some new phones. Right now what I do is just delete all the phones a contact has and re-create them according to what was posted by the form, but I feel that's not the best way to do it, I also don't think looping over the existing ones and comparing with the posted values and doing a manual diff is the best way to do it either, is there a best practice on how to deal with this?</li> </ul> <p>Thanks, hopefully the questions are clear.</p> <p>[edit] Just for more information, phone information can be added and deleted dynamically using javascript (jquery) within the form [/edit]</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload