简体   繁体   English

Symfony Doctrine 阵列结果的展平阵列结果

[英]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.每个 object 都有一些属性,如 id 和 name 等。但我想要的是仅使用每个 object 的 id 展平整个数组。

What I want is this (flatten the array only with ID's):我想要的是这个(仅使用 ID 展平数组):

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: 一旦你得到标识符数组[0 => ['id' => 1], 1 => ['id' => 6], 2 => ['id' => 26] ...]只是你有使用array_column函数从输入数组中的单个列获取值:

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

Since PHP 5.5.0 自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. 可能是findBy()getResult()方法的结果。 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 最好的做法是使用symfony本地方法,所以Tomasz Madeyski说,使用

...->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 :. 一个好的做法也是创建方法INTO存储库类,并通过给出两种类型的选择使其混合:例如:。

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根据用户KicksheepSon评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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