Grails:在控制器和视图中使用列表


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的投票。
如何在视图中显示。
你能给我一个主意吗?

映射看起来比它应该的复杂。VoteCandidate没有直接关系,而您想要列出每个投票的候选人。

VoteCandidate的唯一联系是通过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实际表示什么?您能否详细说明每个域类的使用,主要是VoteApplication ?为什么不用复数来表示hasMany呢?

相关内容

  • 没有找到相关文章

最新更新