class Candidate {
String username;
static HasMany=[applications:Application]
}
class Vote {
String name;
Date firstdate;
Date enddate ;
static HAsMany=[applications:Application]
}
class Application {
Date datedemand;
Candidate candidate;
Vote vote;
static belongsTo = [candidate:Candidate,vote:Vote]
}
我想要显示投票列表,如果我点击一个投票,它会显示该投票的候选人列表。
我开始了以下尝试,但仍然被阻止:
def candidatsGrpByVte(){
def results = Application.withCriteria {
createAlias("vote", "vote")
projections {
groupProperty("vote.name")
}
}
}
def candidatesByVName(def vname){
def results= Application.createCriteria().list() {
createAlias("candidate","candidatAlias")
createAlias("vote","voteAlias")
eq('voteAlias.name',vname)
projections {
property('candidatAlias.username')
}
}
return results;
}
我想看到候选人在Application的投票。
如何在视图中显示。
你能给我一个主意吗?
映射看起来比它应该的复杂。Vote
与Candidate
没有直接关系,而您想要列出每个投票的候选人。
Vote
与Candidate
的唯一联系是通过Application
。所以你必须为Vote
抓取Application
,并为Application
显示所有Candidate
。
//Lazily:-
def voteCandidature = [:]
Vote.list()?.each{vote->
voteCandidature << [(vote.name) : vote.application?.candidate?.asList()]
}
//Eagerly:-
def candidatesByVote(){
def results = Application.createCriteria().list{
vote{
}
candidate{
}
}
}
results.each{application->
def votes = application.vote
def candidates = application.candidate
//Lost after this
//You have bunch of votes and bunch of candidates
//but both belong to the same application
}
Vote
实际表示什么?您能否详细说明每个域类的使用,主要是Vote
和Application
?为什么不用复数来表示hasMany
呢?