我正在尝试创建一个新的 Project 实例(代码在下面给出),但约束不允许我这样做,因为项目不会看到名称,而是生成 id。例如,它得到的不是 'On target'(name),而是 '3'('On target' name 的 id)。我的同事告诉我创建一个服务,该服务将根据 id 返回属性的实际名称,并从 ProjectController 调用该服务。我对 groovy 和 grails 语法不是很熟悉,所以我不知道该怎么做。
我有一个带有 name 属性的 Stage 域类。在我的引导程序中,我为阶段加载了不同的名称。
if (Stage.count() == 0) {
new Stage(name: "").save()
new Stage(name: "Not Started").save()
new Stage(name: "On target").save()
new Stage(name: "Off target").save()
new Stage(name: "Late").save()
new Stage(name: "Critically Late").save()
new Stage(name: "Complete").save()
}
我还有一个具有 Stage 属性的 Project 类,并且我对这些阶段应用了一些约束:
class Project {
String name
Date dueDate
Date startDate
Stage requirements
Stage design
Stage development
Stage qa
Stage ua
Stage delivery
Date deliveryDue
static constraints = {
name blank: false, unique: true
dueDate min: new Date() - 1, max: new Date() + 365 * 10
startDate min: new Date() - 1, max: new Date() + 365 * 10
requirements inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
design inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
development inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
qa inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
ua inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
delivery inList: ["Not started", "Critically Late", "Off Target Date", "On target"]
}
}