|
Hi group, i need your help and advices, i want to add a simple method to a domain class something like
def fullName = { } |
|
2012/7/30 chichibek bros <[hidden email]> Hi group, i need your help and advices, i want to add a simple method to a domain class something like is this a correct implementation and is this a best practice? Thanks from now! |
|
Hi,
Your implementation is correct but you can get more GString, return statement is optional and use a normal method : String fullName() { "$firstName $lastName"
} but closure will also work Seb On Mon, Jul 30, 2012 at 5:25 PM, chichibek bros <[hidden email]> wrote:
|
|
In reply to this post by chichibek bros
It's perfectly valid to add methods to domain classes. But that's not a method, it's a Closure.
This is how I'd do it: String fullName() { "$firstName $lastName" } Burt
|
|
In reply to this post by chichibek bros
As others have commented, you are not using Groovy idioms and you've defined a closure instead of a method. You should do: def fullName { "$firstName $lastName"} A good Groovy book can help you develop good Groovy idiom. Jean Duteau
|
|
You may want to add it to transients
also so that GORM doesn't try to save your derived field.
static transients = ['fullName'] Regards, Greg On 31/07/12 8:41 AM, Jean Duteau wrote:
--
|
||||||||||
|
Thanks for your answer, kind regards
2012/7/30 Greg Pagendam-Turner <[hidden email]>
|
||||||||||
|
If you make the method getFullName() { "$firstName $lastName"}
You can refer to it as myDomainInstance.fullName. You also don't need to add this to transients. As I understand it only if add both a getter and a setter would you need to add it to transients. I have several methods in my domain classes defined as getSomething() and don't add them to transients with no problems. |
|
I forgot the return type, so the method would be:
String getFullName() {"$firstName $lastName"} |
|
In reply to this post by sethfuller
some days ago a symfony framework developer friend told me if in grails is there a place were you can create all sql queries and call it like getAllSomething() but not in controllers after some of your answer i was wondering if the domain class is that place?
2012/7/31 sethfuller <[hidden email]> If you make the method getFullName() { "$firstName $lastName"} |
|
In reply to this post by chichibek bros
That's true in 2.0+. Burt sethfuller <[hidden email]> wrote: If you make the method getFullName() { "$firstName $lastName"} You can refer to it as myDomainInstance.fullName. You also don't need to add this to transients. As I understand it only if add both a getter and a setter would you need to add it to transients. I have several methods in my domain classes defined as getSomething() and don't add them to transients with no problems. -- View this message in context: http://grails.1312388.n4.nabble.com/how-to-add-a-method-to-a-domain-class-tp4632425p4632434.html Sent from the Grails - user mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
I didn't realize that was only 2.0+. I guess that's why you didn't suggest it in your original answer.
|
|
In reply to this post by chichibek bros
It may depend on your version of Grails. I know in 2.0+ it is good to have getAllSomething() type methods in domain classes.
|
| Powered by Nabble | Edit this page |
