1
votes

I'm using mongoDB in my symfony3 application, and I need to use setMaxResults and setFirstResult in my repository to paginate my results in my listing action. But I'm more used to use MySql as a database and I don't find how to do it in my cas.

I tried to use it as this :

$qb = $this->createQueryBuilder('DemoBundle:Entity');
$qb ->select('u');
$qb ->setMaxResults($max) 
    ->setFirstResult($first); 

But I have an error as followed :

Attempted to call an undefined method named "setMaxResults" of class "Doctrine\ODM\MongoDB\Query\Builder

The full function is as like this :

public function search($data, $page = 0, $max = NULL, $getResult = true) 
{ 
    $qb = $this->createQueryBuilder('AresAPITournamentBundle:Tournament');
    $query = isset($data['query']) && $data['query']?$data['query']:null; 

    $qb ->select('u'); 

    if ($query) { 
        $qb 
            ->andWhere('u.name like :query') 
            ->setParameter('query', "%".$query."%") 
        ; 
    } 

    if ($max) { 
        $qb ->setMaxResults($max) 
            ->setFirstResult($page * $max) 
        ; 
    } else { 
        $preparedQuery = $qb->getQuery();
    } 


    return $getResult?$preparedQuery->execute():$preparedQuery; 
} 

I found the ressource in this tutorial.

How can I achieve this ?

1

1 Answers

1
votes

Response was :

    $qb ->limit($to)
        ->skip($from);