Grails:在Controller和View中使用列表

时间:2013-05-25 01:40:56

标签: grails groovy criteria gsp jointable

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

我想在申请表中看到候选人 我如何在视图中显示。
你能给我一个想法吗?

1 个答案:

答案 0 :(得分:0)

映射看起来很复杂。 VoteCandidate没有直接关系,您希望每次投票都列出候选人。

VoteCandidate相关的唯一方式是Application。因此,您必须获取Application的{​​{1}}并显示Vote的所有Candidate

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 } 实际上是什么意思?您能否详细说明每个使用的域类,主要是VoteVote?为什么不为Application使用复数?