【问题标题】:Cascading update between fields in Grails controllerGrails 控制器中字段之间的级联更新
【发布时间】:2014-04-26 12:33:55
【问题描述】:

我有一个名为 Merchant 的类,它有两个相关字段(在域和数据库中都相关),如下所示:

/**
*This class keeps information about a merchant
*/
class Merchant{

...

//The email of the merchant which is also 
//as the username property of the login field
String email
//The login credentials of the merchant
UserLogin login

...

}

Userlogin 类也是这样定义的

/**
*This class is used to hold the login credentials 
* of a Customer, Admin or "Merchant"
*/
class UserLogin{

...

//The username used to Login
String username
//The password used to Login
String password

...

}

现在的问题是,在添加新商户的过程中,还会为商户生成一个登录,使用他的电子邮件作为用户名。

我正在使用动态脚手架,但只想添加代码来更新商家的 userLogin 的用户名字段。

现在,当一个商家的邮箱被更新时,登录的用户名没有更新,我通常在登录后使用用户名来获取相应的商家。

  • 如何添加一个小代码 sn-p 在更改电子邮件时更新商家的登录凭据(用户名),或者我可以在商家的电子邮件和用户名之间创建数据库更新级联的任何方式他的登录名。(我可以在我覆盖的控制器操作中使用 super.update() 吗?

【问题讨论】:

    标签: grails grails-orm grails-controller


    【解决方案1】:

    在您的 Merchant 域类中,您可以添加以下方法来拦截更新事件,如果电子邮件属性已更改,则更新相应的用户名。在我的脑海中,它看起来像这样:

    def beforeUpdate() {
        if (this.getDirtyPropertyNames().contains('email')) {
            this.login.username = this.email
        }
    }
    

    您可以在documentation 中找到有关 GORM 事件及其挂钩的更多信息。

    【讨论】:

    • 非常感谢您的回答
    • 不用担心。很高兴为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多