3

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:) ????

Tyler Evans
  • 567
  • 1
  • 8
  • 25
  • Ever found an answer to this one? I have a similar question here http://stackoverflow.com/questions/15250974/createcriteria-with-projections-does-not-select-all-columns – Sap Mar 07 '13 at 07:30

1 Answers1

0

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.

Joseph
  • 1,442
  • 21
  • 44
  • i want to project both the id and name , but for some reason only the name is being projected when i test the function.... – Tyler Evans Dec 18 '12 at 04:14
  • hi joseph thanks for the help... this still doesn't do anything - i have no idea why? I am only wanting to return the id and name, and not everything else from my domain (hence projections). The domain hasMany lots of items and so if i dont use projections at all... it returns everything (huge file), and when i just project the id,name as you showed me above, I get the problem in my original question... – Tyler Evans Dec 19 '12 at 01:15
  • I would add SQL logging into your code to see what the query is printing off. http://stackoverflow.com/questions/2568507/how-to-log-sql-statements-in-grails – Joseph Dec 19 '12 at 17:48