问题
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]
}
I want to display the list of votes and if I click on a vote, it displays the list of candidates for this vote.
I started the following attempt, and I remain blocked :
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;
}
I want to see the candidates in a vote from Application.
how I displayed in a view.
can you give me an idea ?
回答1:
The mappings look complex than it should be. Vote is not directly related to Candidate and you want to list candidates per vote.
The only way Vote is related to Candidate is through Application. So you have to grab the Application for a Vote and show all the Candidates for the Application.
//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
}
What does Vote signify actually? Can you elaborate on each of the domain classes use, mainly Vote and Application? And why don't you use plurals for hasMany?
来源:https://stackoverflow.com/questions/16745620/grails-use-list-in-controller-and-view