[英]Multiple domain objects creation in Grails
I would like to create around 30 domain objects inside a controller. 我想在控制器内创建大约30个域对象。 Here is how I wrote for the first object and it works fine (can see the output on index.gsp). 这是我为第一个对象编写的方式,它工作正常(可以在index.gsp上看到输出)。 Now, if I have to do the same for the 30 objects I need, should I have 30 different names or is there a simpler way? 现在,如果必须对所需的30个对象执行相同的操作,是否应该使用30个不同的名称,或者有更简单的方法吗?
class VendorController {
def index() {
def myvendor = new Vendor(name: "myVendor")
[vendor: vendor]
}
I know I can create objects in the BootStrap.groovy, but that isn't working (check Grails error: table or view does not exist ) Until I figure out the mistake there, I want to create the objects in the controllers. 我知道我可以在BootStrap.groovy中创建对象,但是那是行不通的(检查Grails错误:表或视图不存在 ),直到我找出那里的错误,我才想在控制器中创建对象。
I'd do this in a service with a transaction, but that aside, you can do the following syntax: 我会在带有事务的服务中执行此操作,但除此之外,您可以执行以下语法:
(1..10).each { idx ->
new Vendor(name: "myVendor_${idx}").save()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.