I have an image which belongs to a user
class Image {
static belongsTo = [user: User]
String name
String path
}
class User {
String name
}
How can I get the user Id without fetching the user in the database :
myService.doIt(Image.get(1).user.id)
fetches the user (I just need the id here)
Use the dynamic "fooId" property for a domain class property "foo":
myService.doIt(Image.get(1).userId)
Related
I have created two domain class named City and Address in Grails 3:
class City {
Long id
static hasMany = [addresses: Address]
static mapping = {
id generator: "increment"
}
}
class Address {
Long id
}
I have created two controllers for these classes. The controllers just extend the RestfulController class. I have created the following URL mapping:
group("/api") {
"/city"(resources: 'city') {
"/addresses"(controller: 'address')
}
"/address"(resources: 'address')
}
The idea being that I can access the addresses belonging to a city (with id = 1) via localhost:8080/api/city/1/addresses. Unfortunately, I always get all addresses and not only the ones belonging to the given City. If I break on the index method of the AddressController I see that the params given to the method indclude a cityId which contains the id of the City passed in the query. However, it seems that the id is not picked up during the database query. Am I doing something wrong here? Do I need to add a belongsTo / mappedBy or a City to the Address?
I'm using grails and I have the following domain classes:
class User {
transient springSecurityService
String password
static belongsTo = [person: Person]
}
And
class Person {
String name
String emailAddress
....
}
I would like to use the person object's email address as the username in Spring Security.
According to the Spring Security manual, that just takes setting the property of grails.plugins.springsecurity.userLookup.usernamePropertyName to the non-"username" field.
I've tried person.emailAddress, and that doesn't work.
How can I get Spring security to use the Person reference to the User? I can't put the password on the Person class, and I've tried to use inheritance (that brings up other issues).
So I've traced this down to GormUserDetailsService, where this is being called and implimented. The code that is used to find the user is:
def user = User.findWhere((conf.userLookup.usernamePropertyName): username)
if (!user) {
log.warn "User not found: $username"
How would I structure the userNamePropertyName so that I could get it look through the child property?
It's bad idea. Just replace email to User, but if you want to use this field in person, create link to this field.
class User {
transient springSecurityService
String password
String email
static belongsTo = [person: Person]
}
and Person:
class Person {
String name
String emailAddress
....
String getEmail(){
user.email //if relations OneToOne
}
static hasOne = [user:User] //if relations OneToOne
}
You should stay relations OneToOne, because I can't imaginate in what case it should be OneToMany
UPD. Or you can try try this one:
class User extends Person{..}
and in spring security config write just email, but i'm not sure.
I'm trying to fetch a user profile picture from the DB by calling the getProfiePicture() method on User object.
Method call from view:
<p>${user.getProfilePicture()}</p>
User.groovy domain class:
class Picture {
User user
String url
boolean profile = false
boolean verified = true
static belongsTo = [user: User]
static constraints = {
}
}
Picture.groovy domain class:
class User {
static hasMany = [pictures: Picture]
String uid = UUID.randomUUID().toString()
String username
String password
String getProfilePicture() {
log.debug("ID: " + id)
log.debug("UID: " + uid)
log.debug("Pictures: " + pictures)
return Picture.findByUserIdAndProfile(id, true)
}
}
Picture table:
Problem
I'm getting this error when I'm trying to get the profile picture:
Class
org.codehaus.groovy.grails.exceptions.InvalidPropertyException
Message
No property found for name [userId] for class [class foo.Picture]
What am I doing wrong?
I'm using:
Grails 2.4.4
The getProfilePicture() method inside your User domain class should return the following:
Picture.findByUserAndProfile(this, true)
The reason you're getting that error is because you are trying to find a Picture instance by userId, a field which doesn't exist.
You can implement getProfilePicture as following:
String getProfilePicture() {
Picture.where { user == this && profile == true}.first().url
}
Alternatively you can use a dynamic Finder (as shown in the comments). By the way: the Picture Class does not need to create the user Attributes twice. static belongsTo = [user: User] is enough.
I have this domain classes, let's say:
class Person {
String name
Integer age
//car data that needs to be shown and filled in views
//but not persisted in Person class
String model
String color
static afterInsert = {
def car = new Car(model: model, color: color)
car.save()
}
}
class Car {
String model
String color
}
What I need is to show in my Person views (create and edit) the model and color properties that are defined inside Person class but these doesn't have to be persisted with this class. These data, model and color, have to be persisted using the Car domain class maybe using the afterInsert event. In other words, I need to save data from a domain class using the views from another domain class.
Thanks in advance.
You can use transients on properties you want GORM to ignore, for example
class Person {
static transients = ['model', 'color']
String name
Integer age
//car data that needs to be shown and filled in views
//but not persisted in Person class
String model
String color
..
}
Just curious but is there a reason you're not using associations
class Person {
..
static hasMany = [cars: Car]
}
class Car {
..
static belongsTo = [Person]
static hasMany = [drivers: Person]
}
.. or composition
class Person {
Car car
}
or simply data binding with multiple domains
//params passed to controller
/personCarController/save?person.name=John&age=30&car.model=honda&car.color=red
//in your controller
def person = new Person(params.person)
def car = new Car(params.car)
I have a domain class like:
class Product {
String name
String number
}
and I only want name field be created in the database as a column, I will generate the number field in the code, I don't want it to be a column of the Product table in the database.
What's the best way to do that?
class Author {
String name
String getUpperCaseName() { name.toUpperCase() }
static transients = ['upperCaseName']
}
See http://www.grails.org/doc/latest/ref/Domain%20Classes/transients.html