How to get a Collection in Doctrine2's query results

前端 未结 3 2070
误落风尘
误落风尘 2021-01-31 02:42

I am trying to execute a query using doctrine2 and need it to return a collection object.

Simplified snippet:

$players = $this->getEntityManager()
            


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-31 03:46

    The getResult() always returns an array. If you want a collection, you must pass the array that is returned by getResult() to Doctrine's ArrayCollection

    e.g.

    use Doctrine\Common\Collections\ArrayCollection;
    
    $result = $this
        ->getEntityManager()
        ->createQueryBuilder()
        ->select('p')
        ->from('...\Player', 'p')
        ->getQuery()
        ->getResult()
    ;
    
    $players = new ArrayCollection($result);
    

提交回复
热议问题