Search code examples
symfony4doctrine-odm

Doctrine ODM/SF4. 1: Must return results instead of cursor


I am trying to use query builder of doctrine odm with Symfony 4.1. I have created a repository :

public function myFunction(Foo $foo)
{
    $query Builder = $this->createQueryBuilder();
    $queryBuilder
      ->eagerCursor(true) 
       ->field('foo')->references($foo)
       ->field('date')
       ->lte('2018-10-19 23:59:59')
       ->gte('2018-10-19 00:00:00');

    return $queryBuilder;

}

In my Controller :

$bars = $this->dm->getRepository(Bar::class)->myFunction($foo)->getQuery()->execute();

return new JsonResponse($bars);

Unfortunately, this returns me a EagerCursor class instead of my Array Collection. I also tried toArray with no result..

I have made some searches here, on Google and on doctrine Doc, in vain.

Do you have any tip for getting my ArrayCollection returned like doctrine or please?


Solution

  • I'm not sure why you'd need an ArrayCollection just to stuff it into JSONResponse but you need to create it manually:

    $bars = new ArrayCollection($this->dm->getRepository(Bar::class)->myFunction($foo)->getQuery()->execute()->toArray());