开发者

Load domain instances from m:n relationship in Grails

Background:

Consider the following model:

  • A Map can be in one or more Mapsets
  • A Mapset must contain one or more Maps

I've modeled it like this:

Map.groovy:

static belongsTo = [ User, Mapset ]
static hasMany = [ mapsets : Mapset ]

Mapset.groovy:

static hasMany = [ maps : Map ]

Problem:

I'm in the Map controller, and I have a parameter named set, which designates the set I want to load the maps from.

Normally, my Map controller loads the maps and shows them on a paginated view, like this:

def maps = Map.createCriteria().list(max: params.max, offset: para开发者_开发知识库ms.offset, sort: params.sort, order: params.order) { }

But in order to be more selective, I'd only like to get the maps that belong to one Mapset.

Question:

How do I load only the maps that belong to the Mapset with id = 1? I need the pagination parameters though.

I figured I could do something like that:

Mapset.get(params.set).maps

but what then?


[UPDATE]

def maps = Map.createCriteria().list(max: params.max, offset: params.offset, sort: params.sort, order: params.order){
   mapsets {
       eq('id', params.set)
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜