Return array, not object from Doctrine query - Symfony2

前端 未结 3 677
伪装坚强ぢ
伪装坚强ぢ 2021-02-12 20:02

I\'m using this:

$this->getDoctrine()->getRepository(\'MyBundle:MyEntity\')->findAll(array(), Query::HYDRATE_ARRAY);

I thought that sh

相关标签:
3条回答
  • 2021-02-12 20:38

    Use getScalarResult() to get an array result with objects truncated to strings.

    0 讨论(0)
  • 2021-02-12 21:02

    According to this EntityRepository class, findAll don't take multiple arguments.

    The code below should do what you want

    $result = $this->getDoctrine()
                   ->getRepository('MyBundle:MyEntity')
                   ->createQueryBuilder('e')
                   ->select('e')
                   ->getQuery()
                   ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
    
    0 讨论(0)
  • 2021-02-12 21:03

    You can also use the getArrayResult() function instead of getResult(). It returns an array of data instead:

    $query = $em->createQuery("SELECT test FROM namespaceTestBundle:Test test");
    $tests = $query->getArrayResult();
    
    0 讨论(0)
提交回复
热议问题