Search code examples
mysqlgrailsgrails-2.0grails-domain-classgrails-controller

How to save multiple child values with more than 2 columns in grails


I have a domain named Committee which is parent. And another domain is committeeMembers and it is child.

 class Committee {
    Long id
    Division division
    District district
    Upazila upazila
    UnionParishad unionParishad
    Ward ward
    CommitteeType committeeType
    String name
    int memberAmount
    Date entryDate=new Date()
}

class CommitteeMembers {
    Committee committee
    CommitteeMemberDesignation committeeMemberDesignation
    String name
    String address
    String mobileNo
    String  email
}

Now I need to save a committee with it's member into database:

 def addNewCommitteeAndMembers(){
    Committee committee = new Committee()
    committee.division = Division.get(params.divisionId?.toLong())
    committee.district = District.get(params.districtId?.toLong())
    committee.name = params.wcName
    committee.presidentName = params.presidentName
    committee.unionParishad = UnionParishad.get(params.upId?.toLong())
    committee.upazila = Upazila.get(params.upazilaId?.toLong())
    committee.ward = Ward.get(params.wardId?.toLong())

    List memberNames = params.list('memberName')
    committee.memberAmount = memberNames.size()

    committee.save()

    def committeeId = Committee.get(committee.id)
    memberNames.each {
        CommitteeMembers committeeMembers = new CommitteeMembers()
        committeeMembers.name = it
        committeeMembers.committee = committeeId
        committeeMembers.save()
    }

    message = "Committee & Members are saved successfully"
}

But my committee is saving but how can I save my committeeMembers values with it. Suppose I have 2 fields in committeeMembers as name and address.

How can I save these two fields with the committee id now? I have no idea how to do it. I have saved only one field for so many committeeMembers but not for more than 1.

I have tried a lot and goggled for it much. But no positive results. Can anyone please help me on this ?


Solution

  • It's difficult to answer without the domain classes but I think you have the following domain classes:

    class Committee {
        ...
        static hasMany = [members: CommitteeMembers]
        ...
    }
    
    class CommitteeMember {
        String name
    
        static belongsTo = [committee: Committee]
    }   
    

    In this case, to persist a Committee with N CommitteeMembers you have to:

    def committee = new Committee()
    // Set all the fields in Committee
    committe.name = ...
    committe.district = ...
    ...
    
    List memberNames = params.list('memberName')
    memberNames.each { String memberName ->
        def committeeMember = new CommitteeMember()
        committeeMember.name = memberName
    
        committee.addToMembers(committeeMember)
    }
    
    committee.save()
    

    Please note that you only have to add the members to the committe and then save the committe. You don't have to save every member. Grails will do that for you.