简体   繁体   中英

Multiple domain objects creation in Grails

I would like to create around 30 domain objects inside a controller. Here is how I wrote for the first object and it works fine (can see the output on 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?

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.

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM