Symfony2,主义2:getResult对象 [英] Symfony2, Doctrine 2: getResult Object
问题描述
$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 )
而不是像这样的简单数组:
instead of a simple array like this:
array ( [id] => 1,
[title] => "something",
[body] => "content" )
我在Symfony 2中使用它。
I use it with Symfony 2.
推荐答案
您在这里有几个选择。据我所知,默认情况下,您无法从实体存储库中找到数组形式的结果。相反,您可以执行以下两项操作之一:
You have a couple options here. As far as I know, you can't find results as arrays from entity repositories by default. Instead, you can do one of two things:
首先,您可以在自己的计算机上实现 toArray()
方法实体对象(可能通过映射的超类 ),它仅返回属性数组。
First, you could implement a toArray()
method on your entity object (perhaps through a mapped superclass) that simply returns an array of properties.
第二,您可以使用Doctrine查询语言通过 getArrayResult()获取所需的信息。
方法,也许像这样:
Second, you could use Doctrine Query Language to pull the information that you need using the getArrayResult()
method, perhaps something like this:
$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);
可以在此处。
这篇关于Symfony2,主义2:getResult对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!