简体   繁体   中英

creating new instance of domain class not saving

Rather than save an editted domain class, I want to create a new instance of it with the changes I have enacted.

def update =
{
        def VariantInstance = Variant.get(params.id)
        VariantInstance.properties = params
        def NewVariantInstance = new Variant()
        //Now Assign Variant Instance new Values
        NewVariantInstance.Name = VariantInstance.Name
        NewVariantInstance.LocationID = VariantInstance.LocationID
        NewVariantInstance.aliases = VariantInstance.aliases
        NewVariantInstance.closeToBoundary = VariantInstance.closeToBoundary


        if (VariantInstance) {
        if (!VariantInstance.hasErrors() && !NewVariantInstance.hasErrors()) {
            println("no errors")
            try {
            NewVariantInstance.save()
            }
            catch (Exception ex)
            {
                println(ex.toString())
            }


            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'Variant.uniqueIdentifyingName', default: 'Variant'), VariantInstance.id])}"
            redirect(action: "list")
        }

}

However when I run through this A) A new version is not saved and B) the NewVariantInstance does not have an id. Thoughts?

Your VariantInstance and NewVariantInstance are never validated, so hasErrors() will always return false. You need to switch from

!VariantInstance.hasErrors() && !NewVariantInstance.hasErrors()

to

VariantInstance.validate() && NewVariantInstance.validate()

It is likely that NewVariantInstance has a validation error so the save() is not successful.

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