Grails

Lists/Sets/Maps created by hasMany declarations should support dynamic findBy/countBy methods

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Won't Fix
  • Affects Version/s: 0.5
  • Fix Version/s: None
  • Component/s: Persistence
  • Labels:
    None
  • Environment:
    Any

Description

Given

class Widget {
String name
static hasMany = [knobs:Knob]
}

class Knob {
String name
String color
Widget widget
static belongsTo = Widget
}

The following should be supported:

def widget = Widget.findByName("foo")
def blueknobs = widget?.knobs.findAllByColor("blue")

returning the knobs belonging to widget "foo" that are blue.

Currently, you must invert the query, losing DRY-ness and expressiveness:

def widget = Widget.findByName("foo")
def blueknobs = Knob.findAllByWidgetAndColor(widget, "blue")

Activity

Hide
Jörg Gottschling added a comment -

I also came around this issues. In my case I wanted to show a paginated list of a one-to-many relationship. Applied to you classes above it would be:

WidgetController {
...
def showKnops = {
def widget = Widget.get(params.id)
if(!widget) {
...
} else {
// I have to do
def knobs = Knobs.findAllByWidget(widget, params)
// I want to do / my suggestion
def knobs = widget.listKnobs(params)
// Or with the suggestion above from Clay
def knobs = widget.knobs.list(params)
return [widgetInstance:widget, knobList:knobs]
}
}
}

I also thinking about that this Controller Method should be part of the default scaffolding. I will create another issue about that.

Show
Jörg Gottschling added a comment - I also came around this issues. In my case I wanted to show a paginated list of a one-to-many relationship. Applied to you classes above it would be: WidgetController { ... def showKnops = { def widget = Widget.get(params.id) if(!widget) { ... } else { // I have to do def knobs = Knobs.findAllByWidget(widget, params) // I want to do / my suggestion def knobs = widget.listKnobs(params) // Or with the suggestion above from Clay def knobs = widget.knobs.list(params) return [widgetInstance:widget, knobList:knobs] } } } I also thinking about that this Controller Method should be part of the default scaffolding. I will create another issue about that.
Hide
Graeme Rocher added a comment -

Moving non-critical issues that aren't going to make it into 1.1 to 1.2

Show
Graeme Rocher added a comment - Moving non-critical issues that aren't going to make it into 1.1 to 1.2
Hide
Graeme Rocher added a comment -

There is no scope / time to resolve these remaining lower priority issues for 1.2 so moving to 1.3

for 1.2 final only issues considered blocking will now be fixed

Show
Graeme Rocher added a comment - There is no scope / time to resolve these remaining lower priority issues for 1.2 so moving to 1.3 for 1.2 final only issues considered blocking will now be fixed
Hide
Burt Beckwith added a comment -

Once you have a Widget instance you can easily find instances using regular Groovy methods:

def blueKnobs = widget.knobs.findAll { it.color == 'blue' }

int numberOfBlueKnobs = widget.knobs.findAll { it.color == 'blue' }.size()

Show
Burt Beckwith added a comment - Once you have a Widget instance you can easily find instances using regular Groovy methods: def blueKnobs = widget.knobs.findAll { it.color == 'blue' } int numberOfBlueKnobs = widget.knobs.findAll { it.color == 'blue' }.size()

People

Vote (1)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: