Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GORM paginateParams usage problem

I'm just trying to do pagination but until now I couldn't make it. I have 2 domain classes and one to many relationship.

class User {
   static hasMany = [contacts:Contact]
}

class Contact {
   static belongsTo = [ user : User ]
}

I have 20 Contacts.

When I tried to make a query like this :

def maxResult = 20

def startIndex = 0

def contacts = Contact.findAllByUser(user, [max:maxResult, offset:startIndex])

it is not working. Query is working but pagination with gorm is not working. Result is just 1 contact object.

when I tried;

def startIndex = 0

def contacts = Contact.findAllByUser(user, [offset:startIndex])

Result is 20 contact object but when i tried it with different startIndex value, it is not working also. for startIndex = 5, result is also 20 ontact object.

Is there anybody have any idea about this. Maybe i am doing something wrong, maybe it is gorm's problem. I havent found the answer. Thanks for your answers.

like image 787
erimerturk Avatar asked Feb 02 '26 07:02

erimerturk


1 Answers

I haven't tried the DynamicFinder to do this thing yet, but as I view the document, your syntax seems to be right. As an alternative, I use createCriteria to solve the paging problem.

def queryResult = Contact.createCriteria().list(max: max, offset: offset) {
            and {
                /// FILTER ///
                user {
                    eq("id", userInstance.id)
                }
            }
    }
like image 132
Hoàng Long Avatar answered Feb 03 '26 23:02

Hoàng Long



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!