简体   繁体   中英

Flatten Array Result of Symfony Doctrine Array Result

With a repository I got an array result (each array is an entity object) like this:

array(
  0 => object of type entity,
  1 => another object of type entity,
  2 => another object of type entity,
)

each object has some properties like id and name, etc. But I what I want is flatten the whole array only with the id of each object.

What I want is this (flatten the array only with ID's):

Array
(
    [0] => 1
    [1] => 6
    [2] => 23
)

My solution:

$ids = array_map($transform = function($entity) {
    if ($entity instanceof Entity) {
       return $entity->getId();
    }
 }, $myGreatDbResult);

My solution is working but is there a better way to get this result?

Once you get the array of identifiers [0 => ['id' => 1], 1 => ['id' => 6], 2 => ['id' => 26] ...] just you have to use array_column function to get the values from a single column in the input array:

$ids = array_column($result, 'id');

Since PHP 5.5.0

Output:

Array
(
    [0] => 1
    [1] => 6
    [2] => 23
    ...
)

The result you are getting:

array(
  0 => object of type entity,
  1 => another object of type entity,
  2 => another object of type entity,
)

Is probably a result of findBy() or getResult() methods. To achieve what you want you will need to create your own query and do something like this:

$result = $entityManager->createQueryBuilder()
    ->from('AcmeDemoBundle:Entity', 'e') //your entity
    ->select('e.id') //your id field
    ->getQuery()
    ->getScalarResult();

This will give you array you're expecting

The best practices are to use the symfony native method, so as Tomasz Madeyski says, Use

...->getQuery()->getScalarResult()

You can also get it that way by doing

->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY)

A good practice also is to create the method INTO the repository class and make it hybrid by giving the choice of the two types, for exemple :.

public function findAll($hydratedArray = false)
{
    $query = $this  ->createQueryBuilder('p')
                    ->leftJoin('p.company', 'c')
                    ->select('p')
                    ->orderBy('c.name', 'ASC');

    $result = $hydratedArray    ? $query
                                    ->leftJoin('p.series', 's')
                                    ->leftJoin('s.variety', 'v')
                                    ->addSelect('c', 's', 'v')
                                    ->getQuery()
                                    ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY) // or getScalarResult()
                              : $query->getQuery()->getResult();

    return $result;
}

To do what you want you can now simply use

$qb->getSingleColumnResult()

Which returns a numeric array with each column value (omitting the column name etc)

As per comment from user KicksheepSon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM