在Grails域类中使用ENUM [英] Use of ENUMs in Grails Domain Class

查看:136
本文介绍了在Grails域类中使用ENUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails中有几个例子(在SO中也是这样),但是我无法获得所需的结果。

There are a few examples of ENUM on Grails (here in SO as well), but I am not being able to get the desired results.

解决方案包括
1)通过将src / groovy
域类下的独立类中的ENUM / p>

Solutions include 1) By having the ENUM in a separate class under the src/groovy Domain Class

class Offer {
    PaymentMethod acceptedPaymentMethod 
    ..
}

src / groovy 付款方式

public enum PaymentMethod {
    BYBANKTRANSFERINADVANCE('BANKADVANCE'),
    BYINVOICE('ByInvoice'),
     CASH('Cash'),
    CHECKINADVANCE('CheckInAdvance'),
    PAYPAL('PayPal'),
    String id

    PaymentMethod(String id) {
        this.id = id
    }
}

在这种情况下,枚举类根本不会在域类颁发一个错误。看起来像以前在版本2之前用于Grails。

In this case the Enum Class is not recognized at all at the domain class issuing an error. Looked like this used to work for Grails prior to version 2.

我在这里缺少一些东西吗?如何在Grails的域中使用外部ENUM类?

Am I missing something here? How to use external ENUM class in a domain in Grails?

2)将ENUM放在域类中。

2) Place ENUM within the domain class.

在这种情况下,grails在编译时不会抱怨,但是脚手架不包含任何ENUM值的信息(就像在scaffolding过程中所包含的propertyPaymentMethod属性一样)
示例:

In this case the grails does not complain while compiling, but the scaffolding does not include any info the ENUM values (it is like the property acceptedPaymentMethod is not included at all in the scaffolding process) Example:

class Offer {
    PaymentMethod acceptedPaymentMethod 
    ..
    enum PaymentMethod {
        BYBANKTRANSFERINADVANCE('BANKADVANCE'),
        BYINVOICE('ByInvoice'),
        CASH('Cash'),
        CHECKINADVANCE('CheckInAdvance'),
        PAYPAL('PayPal'),
        String id

        PaymentMethod(String id) {
            this.id = id
        }
    }
}

检查数据库表的结构,该字段不是ENUM,而是简单的VarChar:

Checking the structure of the DB Table, the field is not an ENUM but a simple VarChar:

| accepted_payment_method        | varchar(255) | YES  |     | NULL    |                |

是否支持Grails Gorm上的ENUM?

Is there support for ENUMs on Grails Gorm at all?

推荐答案

刚刚尝试使用Grails 2.3.4,它与 src / groovy 方法配合使用:

Just tried with Grails 2.3.4 and it worked with the src/groovy approach:

public enum PaymentMethod {
    BYBANKTRANSFERINADVANCE('BANKADVANCE'),
    BYINVOICE('ByInvoice'),
     CASH('Cash'),
    CHECKINADVANCE('CheckInAdvance'),
    PAYPAL('PayPal'),
    String id

    PaymentMethod(String id) {
        this.id = id
    }
}



grails-app / domain / CustomDomain.groovy



grails-app/domain/CustomDomain.groovy

class CustomDomain {
  PaymentMethod acceptedPaymentMethod
}

然后我运行 grails generate-所有CustomDomain ,这是生成的 _form.gsp

Then I runned grails generate-all CustomDomain, and this is the _form.gsp produced:

<div class="fieldcontain ${hasErrors(bean: customDomain, field: 'acceptedPaymentMethod', 'error')} required">
    <label for="acceptedPaymentMethod">
        <g:message code="customDomain.acceptedPaymentMethod.label" default="Accepted Payment Method" />
        <span class="required-indicator">*</span>
    </label>
    <g:select name="acceptedPaymentMethod" from="${custombinds.PaymentMethod?.values()}" keys="${custombinds.PaymentMethod.values()*.name()}" required="" value="${customDomain?.acceptedPaymentMethod?.name()}"/>
</div>

请注意,在Grails 2.3.x中,脚手架功能已在插件中转换,因此您需要在 BuildConfig.groovy

Note that in Grails 2.3.x the scaffold feature was transformed in plugin, so you need in your BuildConfig.groovy:

compile ":scaffolding:2.0.1"

这篇关于在Grails域类中使用ENUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆