Symfony 4, doctrine, getResult and getArrayResult and getScalarResult return same structure results

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 04:01:29

问题


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

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 !

Below, my Service Class :

<?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(),
        );
    }
}

And the result when I dump the output of "getAllEmail()" :

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(),
    );
}

And the dump output :

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 ?


回答1:


Once you call getResult you have fixed the hydration method to tell Doctrine to return objects. See this page for more discussion of that.

In my opinion, once you have fetched a result set from a relational datastore, it is an odd and rather unpredictable idea that you might fetch the same result multiple times and get different formats of the data.

A better test would be to do the same query multiple times, each time with a different result format.




回答2:


The solution is use my own query without createQueryBuilder : https://symfony.com/doc/current/doctrine.html#querying-with-dql-or-sql




回答3:


I might be late but for further notice, I think the right answer is described here.

You can use getScalarResult() then use array_column like that:

$emailArray = array_column($getScalarResult(), "email");


来源:https://stackoverflow.com/questions/48044986/symfony-4-doctrine-getresult-and-getarrayresult-and-getscalarresult-return-sam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!