Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails 有没有一种方法可以通过mongodb控制gorm中的加载关系?_Grails_Gorm_Grails 3.0_Gorm Mongodb - Fatal编程技术网

Grails 有没有一种方法可以通过mongodb控制gorm中的加载关系?

Grails 有没有一种方法可以通过mongodb控制gorm中的加载关系?,grails,gorm,grails-3.0,gorm-mongodb,Grails,Gorm,Grails 3.0,Gorm Mongodb,我正在用Grails3和mongo构建一个RESTAPI。当我需要封送深度更大的对象图时,我遇到了一个问题 我拥有以下域: class Category extends Resource { /* other fields */ Category parent } class Product extends Resource { /* other fields */ List<Category> categories static has

我正在用Grails3和mongo构建一个RESTAPI。当我需要封送深度更大的对象图时,我遇到了一个问题

我拥有以下域:

class Category extends Resource {
    /* other fields */

    Category parent
}

class Product extends Resource {
    /* other fields */

    List<Category> categories
    static hasMany = [categories: Category]
}
在创建控制器时,我从RestfullController进行扩展。我希望能够获得一个产品,并在返回的json中具有父类的类别

我得到以下结果:

/product/${id} 
{
    id: '...',
    categories: [{
        id: '...',
        name: 'cat1'
    }, {
        id: '...',
        name: 'cat2',
        parent: { id: '...' }
    }]
} 

/category/cat2id 
{
    id: '...',
    name: 'cat2',
    parent: { id: '...' }
}

/category
[{
    id: '...',
    name: 'cat1'
},{
    id: '...',
    name: 'cat5'
},{
    id: '...',
    name: 'cat4',
    parent: {
        id: '...',
        name: 'cat5'
    }
},{
    id: '...',
    name: 'cat3',
    parent: {
        id: '...',
        name: 'cat4',
        parent: {
            id: '...',
            name: 'cat5'
        }
    }
},{
    id: '...',
    name: 'cat2',
    parent: {
        id: '...',
        name: 'cat3',
        parent: {
            id: '...',
            name: 'cat4',
            parent: {
                id: '...',
                name: 'cat5'
            }
        }
    }
}]

为什么Category.list()会加载整个Category对象图,而Category.get()、Product.get()和Product.list()不会加载它?有没有办法控制这种行为?

Grails的工作方式是,它只会呈现已经从数据库加载的关联,因此,您可以呈现一些关联,而不呈现其他关联

除了编写自己的封送拆收器之外,没有内置的方法来控制这种行为。看

/product/${id} 
{
    id: '...',
    categories: [{
        id: '...',
        name: 'cat1'
    }, {
        id: '...',
        name: 'cat2',
        parent: { id: '...' }
    }]
} 

/category/cat2id 
{
    id: '...',
    name: 'cat2',
    parent: { id: '...' }
}

/category
[{
    id: '...',
    name: 'cat1'
},{
    id: '...',
    name: 'cat5'
},{
    id: '...',
    name: 'cat4',
    parent: {
        id: '...',
        name: 'cat5'
    }
},{
    id: '...',
    name: 'cat3',
    parent: {
        id: '...',
        name: 'cat4',
        parent: {
            id: '...',
            name: 'cat5'
        }
    }
},{
    id: '...',
    name: 'cat2',
    parent: {
        id: '...',
        name: 'cat3',
        parent: {
            id: '...',
            name: 'cat4',
            parent: {
                id: '...',
                name: 'cat5'
            }
        }
    }
}]