php 返回数组,而不是来自 Doctrine 查询的对象 - Symfony2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17498637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return array, not object from Doctrine query - Symfony2
提问by user2143356
I'm using this:
我正在使用这个:
$this->getDoctrine()->getRepository('MyBundle:MyEntity')->findAll(array(), Query::HYDRATE_ARRAY);
I thought that should ensure it returns an array of an array, but it still returns an array of objects.
我认为应该确保它返回一个数组的数组,但它仍然返回一个对象数组。
I need the whole result returned as an array of an array so I can do this kind of thing (silly example, but it explains what I mean):
我需要将整个结果作为数组的数组返回,所以我可以做这种事情(愚蠢的例子,但它解释了我的意思):
<?php
$result = $this->getDoctrine()->getRepository('MyBundle:MyEntity')->findAll('return-an-array');
?>
This is the age of the person at the 5th record: <?php echo $result[4]['age']; ?>
回答by Thomas Potaire
According to this EntityRepository class, findAll
don't take multiple arguments.
根据这个EntityRepository 类,findAll
不要使用多个参数。
The code below should do what you want
下面的代码应该做你想做的
$result = $this->getDoctrine()
->getRepository('MyBundle:MyEntity')
->createQueryBuilder('e')
->select('e')
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
回答by Samir Mengadi
You can also use the getArrayResult()
function instead of getResult()
. It returns an array of data instead:
您也可以使用该getArrayResult()
函数代替getResult()
。它返回一个数据数组:
$query = $em->createQuery("SELECT test FROM namespaceTestBundle:Test test");
$tests = $query->getArrayResult();
回答by Eric Amshukov
Use getScalarResult()
to get an array result with objects truncated to strings.
使用getScalarResult()
得到与截断为字符串对象的数组结果。