Reputation: 5345
sorry for this simple question. However, I am confused.
I want to read all Strings from the database.
Basically I am having a EmailSettings emailAddresses = new EmailSettings()
object in my controller and want to read all emails saved in the emailSettings Domain object
. The class looks like that:
class EmailSettings {
String emailAddress
static constraints = {
emailAddress(blank: false, nullable: false, email: true)
}
}
My problem is I do not get the saved email addresses out of the emailAddresses
Object. I tried it with list()
, getEmailAddress
(like emailAddresses.getEmailAddress().toString()
, which is null
, even though in the db ARE email addresses saved under this domain object), but it does not work or I get an exception.
Any suggestion how to get the email addresses, which are saved in the db as an array?
I appreciate your answer!
Upvotes: 0
Views: 52
Reputation: 50245
You are creating a new domain instance by newifying it, which will never give you the results that is persisted in db. To get emailAddress
from all EmailSettings in db, you can use as:
EmailSetting.all*.emailAdress
or
EmailSetting.withCriteria {
projections {
property 'emailAddress'
}
}
or can also use DetachedCriteria with above projection.
UPDATE
First approach will be expensive because all of the rows will be fetched and then emailAddress
from each row will be derived. I totally missed mentioning why I had the second approach with usage of Criteria. Using Projections, you would only get the properties you would need instead of fetching the row itself. Thanks to Graeme.
Upvotes: 1