2011-05-14 46 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" ) 

は、私はあなたがここにカップルのオプションがありsymfonyの2

答えて

9

でそれを使用しています。私が知る限り、デフォルトでエンティティリポジトリからの配列として結果を見つけることはできません。

まず、プロパティの配列を返すだけのエンティティオブジェクト(mapped superclassなど)にtoArray()メソッドを実装することができます。

第二に、あなたはおそらく、このような何かgetArrayResult()方法を使用して必要な情報プルする教義クエリ言語を使用することができます。DQLの

$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); 

より深いドキュメントがhereを見つけることができます。

+10

Sf2/Doctrineがこの非常に一般的な使用事例を予期しないことは残念です。 – Acyra

関連する問題