Quantcast

GORM association multiple match

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

GORM association multiple match

alxndrsn
Hi, can anyone help with this GORM query to match multiple
associations on an object?

        class Zoo {
                String name
                static hasMany = [animals:Animal]

                static namedQueries = {
                        // SEARCH1 match any of a list of animals
                        searchOr { searchAnimals ->
                                or {
                                        searchAnimals.each { name ->
                                                animals {
                                                        eq('name', name)
                                                }
                                        }
                                }
                        }
               
                        // SEARCH2 match ALL of a list of animals
                        searchAnd { searchAnimals ->
                                and {
                                        searchAnimals.each { name ->
                                                animals {
                                                        eq('name', name)
                                                }
                                        }
                                }
                        }
                }
        }

        class Animal {
                String name
        }

SEARCH1 will happily match a zoo with any of a list of animals, but
how should SEARCH2 be written to get zoos which have ALL animals in
the supplied list?

Any help much appreciated.

Cheers,

Alex

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: GORM association multiple match

alxndrsn
(Cross-posted to stackoverflow:
http://stackoverflow.com/questions/10529243/grails-gorm-query-to-match-multiple-associated-objects)

On 9 May 2012 18:25, Alex Anderson <[hidden email]> wrote:

> Hi, can anyone help with this GORM query to match multiple
> associations on an object?
>
>        class Zoo {
>                String name
>                static hasMany = [animals:Animal]
>
>                static namedQueries = {
>                        // SEARCH1 match any of a list of animals
>                        searchOr { searchAnimals ->
>                                or {
>                                        searchAnimals.each { name ->
>                                                animals {
>                                                        eq('name', name)
>                                                }
>                                        }
>                                }
>                        }
>
>                        // SEARCH2 match ALL of a list of animals
>                        searchAnd { searchAnimals ->
>                                and {
>                                        searchAnimals.each { name ->
>                                                animals {
>                                                        eq('name', name)
>                                                }
>                                        }
>                                }
>                        }
>                }
>        }
>
>        class Animal {
>                String name
>        }
>
> SEARCH1 will happily match a zoo with any of a list of animals, but
> how should SEARCH2 be written to get zoos which have ALL animals in
> the supplied list?
>
> Any help much appreciated.
>
> Cheers,
>
> Alex
Loading...