Question

I have a Class named Merchant which have two related fields (related in both domain and database) like so:

/**
*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

...

}

The Userlogin class is also defined like so

/**
*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

...

}

The Issue now is that during the addition of a new merchant, A login is also generated for the merchant using his email as the username.

I am using dynamic scaffolding but just want to add code to also update the username field of the userLogin for the merchant.

Now, when a Merchant's email is updated, The username of the login is not updated and i normally use the username to fetch the corresponding merchant after login.

  • How can I add a small code snippet to update the merchant's login credentials(username) when the email is changed, or is thier any way I can create a database update cascade between the email of the merchant and the username of his login.(Can I use super.update() in a controller action that I override?)
Was it helpful?

Solution

In your Merchant domain class you can add the following method to intercept an update event and if the email property has changed update the corresponding username. Off the top of my head it will look something like this:

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

You can find out more information about GORM events and their hooks in the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top