Doctrine orm Symfony2,原则2:getResult对象

Doctrine orm Symfony2,原则2:getResult对象,doctrine-orm,symfony,Doctrine Orm,Symfony,为什么我得到它 $posts = $em->find('Application\BlogBundle\Entity\Post',1); print_r ($posts); 而不是像这样的简单数组: Barii\BlogBundle\Entity\Post Object ( [id:Barii\BlogBundle\Entity\Post:private] => 1 [title:Application\BlogBundle\Entity\Post:private] => so

为什么我得到它

$posts = $em->find('Application\BlogBundle\Entity\Post',1);
print_r ($posts);
而不是像这样的简单数组:

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一起使用

这里有几个选项。据我所知,默认情况下,您无法从实体存储库中找到作为数组的结果。相反,您可以做以下两件事之一:

首先,您可以在实体对象上实现一个
toArray()
方法(可能通过),该方法只返回一个属性数组

其次,您可以使用条令查询语言,使用
getArrayResult()
方法提取所需的信息,可能类似于以下内容:

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"  )

可以找到关于DQL的更深入的文档。

令人失望的是,Sf2/Doctrine没有预见到这种极其常见的用例。