问题
How to get it so i return all of the projections from the below
def c = Company.createCriteria()
def a = c.list(params){
projections{
property 'id', property 'name'
}
}
if(a.size() == 0)
render "404"
else {
render (contentType: 'text/json'){
totalCount = a.totalCount
data = a
}
}
The result comes out like this:
{"totalCount":2,"data":["company1","company2"]}
Where i need it to be:
{"totalCount":2,"data":[{"class":"org.example.Company","id":1,"name":"company1"},{"class":"org.example.Company","id":2,"name":"company2"}]}
In the company domain i have lots of relationships (some one to one, one to many etc...) my domain looks like the following:
package org.example
import java.sql.Timestamp
class Company {
String name
String abn
String cname
String email
String phone
String position
String address
String city
String postcode
int style
int openbookings;
Date date;
int tokenTotal = 0
int totaltokens
int totalboosts
int totalposts
Timestamp tokenstamp
static hasMany = [users: User, broadcast: Broadcast, bookings: Booking, locations: Location,vimsurvey:VimSurvey,rewards: Reward, tokens: CompanyToken]
static constraints = {
abn nullable: true
date nullable: true
style nullable: true
}
}
Any help would be great:) ????
回答1:
http://grails.org/doc/1.1/ref/Domain%20Classes/createCriteria.html
See the property section under projections: 'property Returns the given property in the returned results'. I don't really get what you are asking for by 'all the projections'.
Are you simply looking to Find all for your domain? Why are you using a projection?
def a = c.list(params){
projections{
property 'id', property 'name'
}
}
should be
def a = c.list(params){
projections{
property 'id'
property 'name'
}
}
In fact, I get a compilation error when I attempt to do it your way. I still feel like it makes more sense to simply get the entire domain itself unless there is a very specific reason not to.
来源:https://stackoverflow.com/questions/13911615/grails-projections-not-returning-all-properties-and-not-grouped