0

I m doing something like this to get the data...

 def prods = Product.executeQuery("select category.id,category.name, avg(competition1Price), avg(competition2Price), avg(onlineCompetitionPrice) from Product group by category.id")
        render prods as JSON

Not the output I'm getting is this..

        [[1,"Colchones y",1657.4784,2071.5,1242.5]]

these are just the values..

I want to use the same query and get key value pair.. like the way you do using findAll(query) But I can't seem to implement this query using findAll()

Please Help

Thanks..

4

1 回答 1

0

这是因为 prods 的实例只是带有查询结果的对象,而不是 Product 或其他域类的实例(您使用的 HQL 不代表域类)。你可以:

  • 考虑将此查询和映射的视图用作域类;
  • 手动构建输出;

第二个选项类似于(未测试):

  def output = [[:]]
  prods.each { result ->
    def prod = ['category.id' : result[0] , 'category.name': result[1]] //and so on...
    output << prod
  }
  render output as JSON

使用第二个选项,要更改生成的结果,只需更改地图的结构。如果需要,您还可以拥有地图列表。

于 2012-09-14T12:16:30.637 回答