gpt4 book ai didi

mysql - 如何在grails中保存超过2列的多个子值

转载 作者:行者123 更新时间:2023-11-29 07:54:34 25 4
gpt4 key购买 nike

我有一个名为“委员会”的域名,它是父域名。另一个域是committeeMembers,它是子域。

 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
}

现在我需要将委员会及其成员保存到数据库中:

 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"
}

但是我的委员会正在节省,但我如何才能用它来保存我的委员会成员的值(value)观。假设我在committeeMembers 中有两个字段作为姓名和地址。

现在如何用委员会 ID 保存这两个字段?我不知道该怎么做。我只为这么多委员会成员保存了一个字段,但没有为超过 1 个保存。

我已经尝试了很多并且对此感到困惑。但没有积极的结果。谁能帮我解决这个问题吗?

最佳答案

如果没有域类,很难回答,但我认为您有以下域类:

class Committee {
...
static hasMany = [members: CommitteeMembers]
...
}

class CommitteeMember {
String name

static belongsTo = [committee: Committee]
}

在这种情况下,要保留具有 N 个 CommitteeMembersCommittee,您必须:

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()

请注意,您只需将成员添加到委员会,然后保存委员会。你不必拯救每个成员。 Grails 会为您做到这一点。

关于mysql - 如何在grails中保存超过2列的多个子值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25506022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com