Symfony 4,学说,getResult和getArrayResult以及getScalarResult返回相同的结构结果 [英] Symfony 4, doctrine, getResult and getArrayResult and getScalarResult return same structure results
问题描述
从symfony 4中,创建一个示例存储库类。在这一堂课中,我创建了一种用于获取所有电子邮件用户列表的方法。我想得到这样的数组结构:
From symfony 4, I create a sample repository class. From this class, I created a method for get the list of all email's users. I would like get a array structure like this :
array(
"email1",
"email2",
"email3",
...
)
但是使用 getResult,我得到了多维数组。然后,我用getArrayResult和getScalarResult进行了测试,每次都获得完全相同的数组结构结果!
But with 'getResult' I get a multidimensional array. Then, I tested with getArrayResult and getScalarResult and I obtain each time exactly the same array structure result !
以下,我的服务类别:
<?php
class UserRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, User::class);
}
public function getAllEmail(){
$result = $this->createQueryBuilder('u')
->select(array('u.email'))
->setMaxResults(5)
->getQuery();
return array( // getResult && getArrayResult && getScalarResult return exactly same array structure
"getResult" => $result->getResult(),
"getArrayResult" => $result->getArrayResult(),
"getScalarResult" => $result->getScalarResult(),
);
}
}
当我转储 getAllEmail的输出时的结果():
And the result when I dump the output of "getAllEmail()" :
为什么要使用getResult / getArrayResult / getScalarResult返回完全相同的数组结构?我在某个地方出错了?
Why getResult / getArrayResult / getScalarResult return exactly same array structure ? I do a mistake somewhere ?
编辑:我修改了存储库类:
Edit : I modified my Repository Class :
public function getAllEmail(){
$result = $this->createQueryBuilder('u','u.email')
->setMaxResults(5)
->getQuery();
return array(
"getResult" => $result->getResult(),
"getArrayResult" => $result->getArrayResult(),
"getScalarResult" => $result->getScalarResult(),
);
}
转储输出:
使用'getResult'和'getArrayResult'我得到了多维数组,在第一个维度中,我收到所有电子邮件(电子邮件是关键)。我的目标更多,但还不完美。我正在寻找一种轻量级的方式(对不起我的英语-_-),我想只收到电子邮件(而不是电子邮件和其他无用的用户信息),因为我想执行最简单的查询。可能吗 ?
With 'getResult' and 'getArrayResult' I get an multidimensional array and in the first dimension, I get all email (emails are the key). I approach more my goal but its not perfect. I'm looking for the 'light weight' way (sorry for my english -_-), I would like get only email (and not email + another useless users information) because I want execute the simplest query as possible. Is it possible ?
推荐答案
我可能来晚了,但为进一步通知,我认为正确的答案已在此处。
I might be late but for further notice, I think the right answer is described here.
您可以使用 getScalarResult()
然后像这样使用 array_column
:
You can use getScalarResult()
then use array_column
like that:
$emailArray = array_column($getScalarResult(), "email");
这篇关于Symfony 4,学说,getResult和getArrayResult以及getScalarResult返回相同的结构结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!