$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()
实体对象上的方法(可能通过 mapped superclass )只返回一个属性数组。
其次,您可以使用 Doctrine Query Language 来使用 getArrayResult()
拉取您需要的信息。方法,也许是这样的:
$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 中找到.
关于doctrine-orm - Symfony2,学说 2 : getResult Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004325/