简体   繁体   English

如何在Grails 3的编辑视图中修改ID列的值?

[英]How to modify the value of the ID column (or columns) in the edit view in Grails 3?

How do you allow the values of the ID columns to be modified at runtime in Grails 3? 您如何允许在Grails 3中在运行时修改ID列的值? In my case, I have a table with a composite primary key consisting of three columns. 就我而言,我有一个表,该表具有由三列组成的复合主键。 I manually specified them to appear in the edit view and my update() method code looks like this: 我手动指定它们显示在编辑视图中,而我的update()方法代码如下所示:

@Transactional
def update(AliasFrequencyDict aliasFrequencyDict) {

    aliasFrequencyDict = AliasFrequencyDict.get( new AliasFrequencyDict(params) )

    if (aliasFrequencyDict == null) {
        notFound()
        return
    }

    aliasFrequencyDict.properties = params

    try {
        aliasFrequencyDict.save(insert: false, flush: true)
    } catch (ValidationException e) {
        respond aliasFrequencyDict.errors, view:'edit'
        return
    }

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.updated.message', args: [message(code: 'aliasFrequencyDict.label', default: 'AliasFrequencyDict'), aliasFrequencyDict.getPK()])
            redirect(action: 'show', params: params)
        }
        '*'{ respond aliasFrequencyDict, [status: OK] }
    }
}

...and when I go into edit view and edit the property not being part of the key, it saves correctly but as soon as I change one of the other ones, I get a message reading that Grails could not found an AliasFreuqencyDict instance of ID null. ...当我进入编辑视图并编辑不属于键的属性时,它可以正确保存,但是当我更改另一个属性时,我收到一条消息,指出Grails找不到以下项的AliasFreuqencyDict实例: ID空。 I guess the problem is that, once you change one of such columns, the ID changes too and Grails somehow doesn't know how to switch from the primary key that the object used to have to the new ones. 我想问题是,一旦更改了其中一列,ID也会更改,Grails不知怎么不知道如何从对象曾经拥有的主键切换到新键。

How should I modify the controller to allow for editing the part-of-key columns too, then? 那么,我应该如何修改控制器以允许编辑键部分列呢?

The (most obvious) problem here is the way you're looking up the one you want to update. (最明显的)问题是您查找要更新的问题的方式。 If you share your domain model for AliasFrequencyDict I can be more specific, but say you have a composite key of First and Last and an instance where [first:"John", last:"Doe"] . 如果您共享AliasFrequencyDict的域模型,我可以更具体一些,但要说您拥有FirstLast的复合键以及一个实例,其中实例为[first:"John", last:"Doe"]

Now you update that to set first=Jane and press update... you are creating a new AliasFrequencyDict for "Jane Doe" and using that in the lookup of .get() , of course that's not going to return an instance of AliasFrequencyDict . 现在,您将其更新为first=Jane并按update ...为“ Jane Doe”创建一个新的AliasFrequencyDict ,并在.get()的查找中使用它,当然,这不会返回AliasFrequencyDict的实例。 If you're changing the value of a PK field, you'll need to pass in the old values to look up the object, and then set the new values accordingly. 如果要更改PK字段的值,则需要传入旧值以查找对象,然后相应地设置新值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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