3

People, I'm facing a problem with grails GORM, my Application is totally dependent of the DomainClass.list() method, it is in all of my create/edit GSPs, but now I need a particular behavior for listing objects. Being more specific I need to filter these lists (All of them) by one attribute.

The problem is I'm hoping not to change all the appearances of these methods calling, so is there a way to customize the behavior of the default list() method ? I need it to function just the way it does, but adding an ending filter.

Nag
  • 1,438
  • 1
  • 11
  • 39
JRafaelM
  • 861
  • 2
  • 10
  • 23
  • 1
    Did you find the solution? I have the exact same problem, I added a DELETED status to my users and I do not want to modify all the code to call a new method, I just want that list() to not return deleted users – Eduard Sep 13 '13 at 09:25
  • @Eduard same problem here. Did you find a solution? – Alexander Suraphel Apr 16 '14 at 08:38

2 Answers2

3

Maybe you can use hibernate filter plugin (see here). This will allow you to filter all finder methods (including list()) based on a property:

static hibernateFilters = {
    enabledFilter(condition: 'deleted=0', default: true)
}
zoran119
  • 10,657
  • 12
  • 46
  • 88
  • 1
    FYI: I tested the hibernate filter plugin and it doesn't seem to work with Grails 2.0. Also, it doesn't work with `get()` calls – Dónal Feb 24 '12 at 13:45
  • 1
    I don't know if it works for me, my problem is kinda complex. The filtering is not directly on domain-classes attributes, it is in a complex relationship, and I do not find out in the plugin docs if it really solves the problem. But this plugin can solve a simpler problem. – JRafaelM Feb 24 '12 at 13:54
0

Have you considered using names queries? You could always do something like this:

class DomainClass {
    // ... class members

    static namedQueries = {
        myList { params->
            // put your complicated logic here
        }
    }
}

Then you can just replace your calls to DomainClass.list() with DomainClass.myList.list().

seth.miller
  • 1,988
  • 17
  • 23