2

我有两张桌子:RegionDistrict。入表region_id也是如此(一个有一个或多个)。因此,当我在我的上选择 a 时,我只想显示与该特定. 我的正确代码显示所有独立于:foreign keyDistrictregiondistrictsregionlistdistrictsregiondistrictsregion

def list = {
        params.max = Math.min(params.max? params.int('max') : 20, 100)
        [districtInstanceList : District.list(params),
        districtInstanceTotal: District.count()]
    } 

有人知道如何仅根据外键约束显示吗?我知道我可以SQL在我的闭包中写一个查询list,但我想 grails 可能有办法做到这一点。我的数据库是MySQLgrails版本是2.0.1。我的District域名是:

class District {

def scaffold = true

String name
String description
String logo
String homepage
// defines the 1:n constrain with the Region table
static belongsTo = [region : Region]
// defines the 1: constraint with the Stream table
static hasMany = [streams : Stream]

static constraints ={
    name(blank:false, minSize:6, maxSize:30)
    description(blank: false, maxSize:100)
}

public String toString(){
    name
}
 }
4

1 回答 1

4

您可以使用 GORM:

def list = {
    params.max = Math.min(params.max? params.int('max') : 20, 100)
    Region region = Region.get(params.id) // or what parameter you're using
    List districts = District.findAllByRegion(region)
    [districtInstanceList : districts,
    districtInstanceTotal: District.count()]
} 

您可以在此处阅读有关 Grails GORM 的信息:http: //grails.org/doc/latest/guide/GORM.html

于 2012-07-07T03:59:32.670 回答