2

と の 2 つのテーブルがRegionありDistrictます。テーブルregion_idforeign keyのも同様ですDistrict( aregionには 1 つまたは複数の がありますdistricts)。そのため、 を選択するregionと、その特定の に関連付けられlistた のみを表示したいと考えています。私の正しいコードは、すべてとは無関係に表示されます:districtsregiondistrictsregion

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

外部キー制約に基づいてのみ表示する方法を誰かが知っていますか? SQLクロージャーにクエリを記述できることはわかっていますがlist、grails にはおそらくそれを行う方法があると思います。私のデータベースはMySQLで、grailsバージョンは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 に答える