本文介绍了带有MongoDB的Grails,Object id和脚手架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据写入mongoDB数据库时遇到了使用集成测试和Grails脚手架的问题。当试图从列表类型页面中选择一个域实例时,出现错误[域名]找不到ID为null。



我确定它是因为Grails的url [controller] / [action] / [id]。此id是一个字符串,需要转换为ObjectId for Grails查询。



有没有办法做到这一点,以便它影响指定的域或甚至更好,所有的域一次吗?

我想我正在写我的应用程序,我可以将它转换为一个ObjectId的行动方法,但我会喜欢脚手架工作或提供一个全球性的解决方案。

解决方案

我也有这个问题。您可以将域对象ID保持为ObjectId并更新控制器,如下所示:



域对象:

  import org.bson.types.ObjectId; 

class DomainObject {
ObjectId id
//添加其他成员变量...
}

控制器:

  def show(String id){
def domainObjectInstance = domainObject.get(new ObjectId(id))
if(!domainObjectInstance){
flash.message = message(code:'default.not.found.message',args:[message (代码:'domainObject.label',默认:'DomainObject'),id])
重定向(操作:列表)
返回
}

[ domainObjectInstance:domainObjectInstance]
}

您还需要更新其他控制器方法id,以及编辑,更新等。



另外,如果您希望grails默认控制器生成对所有域对象像这样工作,您可以将模板更新为coderLMN建议。


I have data writing to a mongoDB database with issues using integration tests and the Grails scaffolding. When trying to select a domain instance from the 'list' type page, I get the error "[domain name] not found with id null".

I am sure it is because of the Grails url [controller]/[action]/[id]. This id is a string and needs to be converted to an ObjectId for Grails queries.

Is there a way to do this so that it affects a specified domain or even better yet, all of the domains at once?

I guess as I'm writing my app, I can convert it to an ObjectId from within the action method, but I'd like to have the scaffolding work or provide a global solution.

解决方案

I had this problem as well. You can keep the domain object id as an ObjectId and update the controller as follows:

domain Object:

import org.bson.types.ObjectId;

class DomainObject {
        ObjectId id
        // Add other member variables...
}

Controller:

def show(String id) {
    def domainObjectInstance = domainObject.get(new ObjectId(id))
    if (!domainObjectInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'domainObject.label', default: 'DomainObject'), id])
        redirect(action: "list")
        return
    }

    [domainObjectInstance: domainObjectInstance]
}

You would also need to update your other controller methods that use id as well such as edit, update etc.

Additionally, if you want the grails default controller generation to work like this for all your domain objects you can update the template as coderLMN suggests.

这篇关于带有MongoDB的Grails,Object id和脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 12:07