Assuming I have a Person and a Status. If Status is like this :
class Status {
String text
Person author
}
I would have done something like this to get the messages list of the current user :
def messages = Status.withCriteria {
author {
eq 'username', currentPerson.username
}
}
But if my relationship in Status is like this, how can I do so?
static belongsTo = [Person]
Thanks for your help.
I tend to use the map notation for belongsTo, so I would do it like this:
class Status {
String text
static belongsTo = [author: Person]
}
Then you're query is easy:
def messages = Status.findAllByAuthor(currentPerson)
If you had bidirectional added into Person with hasMany:
class Person {
static hasMany = [messages: Status]
}
You could also do this:
def messages = currentPerson.messages
Related
I have domain classes as below:
class Account {
String name
String uniqueName
static hasMany = [roles:Role]
}
class Role {
String name
static belongsTo = [account:Account]
static hasMany = [users: User]
}
class User {
String name
}
I received Account's uniqueName from params.uniqueName. And I want to find all users list that has roles that belongsTo account.
I want to use criteria() because I want to do it in pagination.
I try like below code, it's work but it can't do a pagination.
def account = Account.findByUniqueName(params.uniqueName)
def roles = account.roles
[users : roles.users.flatten().unique()]
How should I do this?
Try this one:
add to user:
static belongsTo = [role:Role]
and use this criteria:
User.createCriteria().listDistinct {
role{
account{
eq("uniqueName", params.uniqueName)
}
}
}
You can use criteria like this.
List<Account> results =Account.createCriteria().list
{
eq('uniqueName', params.uniqueName)
maxResults(params.max as int)
firstResult(params.offset as int)
order(params.order, "asc")
}
Try this:
Account account = Account.findByUniqueName(params.uniqueName)
Collection users = []
account.roles.each{ role ->
users += role.users
}
log.info("Users for ${params.uniqueName} account : ${users}"
I'm new to Grails. I'm having problem with retrieving data from DB. I have domains with classes, something like...
class Lightbox {
String name = ''
String link = ''
static hasMany = [users: LightboxUserAccount]
}
class LightboxUserAccount {
UserAccount userAccount
static belongsTo = [lightbox: Lightbox]
}
class UserAccount {
String username
...
}
I want to list all "Lightboxes" owned by user with ID=4. I was trying
def my_lb = Lightbox.findAll("from Lightbox as lb where lb.users=:userAccount", [userAccount: springSecurityService.getCurrentUser()])
or
def my_lb = Lightbox.findAllByUsers(4)
None of those work for me. What am I doing wrong? Thx
Try this one:
Lightbox.findAll("from Lightbox as lb where :userAccount in (lb.users)", [userAccount: springSecurityService.getCurrentUser()])
So I did it slightly different, using criteria instead. Take a look if interested
static getAllByUserAccount(UserAccount userAccount) {
def criteria = Lightbox.createCriteria()
def my_lb = criteria.list {
users {
eq('userAccount', userAccount)
}
}
return my_lb
}
It seems to be working. Thanks for responses though
I have domain
Training
class Training {
static belongsTo = [venue: Venue]
}
Venue
class Venue {
static belongsTo = [city: City]
}
City
class City {
String name
}
now i want to sort Training based on City name .
is there a way to do it in Grails (Gorm)way?
have you tried
def results = Training.withCriteria {
order('venue.city.name', 'asc')
}
Training.list().sort{it.venue.city.name}
def results = Training.list(sort:"venue.city.name", order:"asc")
i have the following (simplified) domain classes
class Filter {
String name
static hasMany = [answers:Answer]
static belongsTo = [user:User]
}
class User {
String name
static hasMany = [answers:Answer, filters:Filter]
}
class Answer {
String text
}
Then i add answers to the user which is working perfectly. The problem occurs when i delete 1 answer of a user:
def delete = {
def answer = Answer.get(params.id)
def users = User.withCriteria() {
answers{
eq("id", answer.id)
}
}
for (user in users)
user.removeFromAnswers(answer)
answer.delete(flush:true)
redirect(action:"index")
}
What happens here is that ALL user --> answer associations get deleted.
I only want to delete this 1 answer and of cause all associations the answer is used.
I know this has to do with the missing belongsTo, but i can't use it because a ansswer can either belong to a user or to an filter...
You can add the belongsTo to set them to nullable:
class Answer {
String text
static belongsTo = [user:User, filter:Filter]
static constraints = {
user nullable:true
filter nullable:true
}
}
and then just delete the Answer directly in the Controller:
def delete = {
def answer = Answer.get(params.id)
answer.delete(flush:true)
}
GORM will take care of the rest the cascading for you.
I use grails-1.3.2 and hbase plugin.
I have some difficulty in creating one-to-Many association with
hbase (i can work with hibernate), so
i decided to try create one-to-Many association with using ArrayList.
Here are my domain classes and controllers:
class Contacts {
String con
static constraints = {}
}
class ContactsController {
def create = {
def contact = new Contacts()
contact.con = params.con
contact.save(flush:true)
}
}
class User {
String firstname
String lastname
// static hasMany = [contact: Contacts]
static ArrayList<Contacts> contact
static constraints = {}
}
class UserController{
def create = {
def user = new User()
user.properties = params
user.save(flush: true)
}
def addContact = {
def user = User.get(params.userID)
def contact = Contacts.get(params.contactID)
user.contact.add(contact)
user.save(flush:true)
}
}
In addContact action user.contact = null, so it can not work.
In user does nor appear contact field.
Can someone help me understand what i have to do for saving ArrayList in db?
I don't know anything about hbase, but the static contact property of the User class looks very suspicious. The fact that this property is static, implies that every user has the same contact list, which seems unlikely to be the desired behaviour.
In a standard GORM domain model - assuming you want each User to have their own contact list - this would be defined
class User {
String firstname
String lastname
static hasMany = [contact: Contacts]
}
Although it looks like we're also defining a static property here, it's actually just the definition of how the Contact and User classes are related (AKA mapping) that is static. The contact property that is dynamically added to the User class is non-static.
Aside
I recommend renaming the Contacts class to Contact and the contact property to contacts. The GORM mapping would then look like this:
class User {
String firstname
String lastname
static hasMany = [contacts: Contact]
}