2014-04-26 41 views
0

我有像這樣一類命名爲商傢俱有兩個相關領域(在這兩個域和數據庫相關的):級聯字段之間更新Grails中控制器

/** 
*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的用戶名字段。

現在,當商家的電子郵件更新時,登錄的用戶名不會更新,我通常使用用戶名登錄後使用用戶名來獲取相應的商家。

  • 我如何添加一小段代碼更新商家的登錄憑據(用戶名)當電子郵件被改變,或者是thier什麼辦法可以創建商家的電子郵件地址和用戶名之間的數據庫更新級聯他登錄。(我可以在控制器操作使用super.update(),我重寫?

回答

1

在Merchant域類,你可以添加以下方法來攔截更新事件,如果電子郵件屬性已更改更新相應的用戶名。關閉我的頭頂,它會是這個樣子:

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

您可以瞭解在documentation GORM事件及其掛鉤的更多信息。

+0

非常感謝您的回答 – JohnTheBeloved

+0

無後顧之憂。樂意效勞。 –