2011-05-14 12 views
5
$posts = $em->find('Application\BlogBundle\Entity\Post',1); 
print_r ($posts); 

왜 내가 가져 왔습니까? 대신 같은 간단한 배열의Symfony2, Doctrine 2 : getResult 객체

Barii\BlogBundle\Entity\Post Object ([id:Barii\BlogBundle\Entity\Post:private] => 1 [title:Application\BlogBundle\Entity\Post:private] => something [body:Application\BlogBundle\Entity\Post:private] => content ) 

:

array ([id] => 1, 
     [title] => "something",    
     [body] => "content" ) 

나는 당신이 여기에 몇 가지 옵션이 있습니다 심포니 2.

답변

9

와 함께 사용. 내가 아는 한 기본적으로 엔티티 리포지토리의 배열로 결과를 찾을 수 없습니다. 대신 다음 두 가지 중 하나를 수행 할 수 있습니다.

먼저 속성 배열을 반환하는 엔터티 개체 (mapped superclass 등)에 toArray() 메서드를 구현할 수 있습니다.

$query = $em->createQuery('SELECT p FROM Application\BlogBundle\Entity\Post p WHERE p.id=:pid'); 
$query->setParameter('tid', $postId); 
$result = $query->getArrayResult(); // shortcut for $query->getResult(Query::HYDRATE_ARRAY); 

더 깊이 DQL에 대한 문서를 찾을 수 있습니다 here :

둘째, 당신은 당신이 getArrayResult() 방법을 사용하여 필요한 정보,이 같은 아마 뭔가를 끌어 교리 쿼리 언어를 사용할 수 있습니다.

+10

Sf2/Doctrine이이 매우 일반적인 사용 사례를 예상하지 않는다는 데 실망합니다. – Acyra