php 如何获取一维标量数组作为学说dql查询结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11657835/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:48:36  来源:igfitidea点击:

How to get a one-dimensional scalar array as a doctrine dql query result?

phpdoctrine-orm

提问by Dawid Ohia

I want to get an array of values from the id column of the Auction table. If this was a raw SQL I would write:

我想从 Auction 表的 id 列中获取一组值。如果这是一个原始 SQL,我会写:

SELECT id FROM auction

But when I do this in Doctrine and execute:

但是当我在 Doctrine 中执行此操作并执行时:

$em->createQuery("SELECT a.id FROM Auction a")->getScalarResult(); 

I get an array like this:

我得到一个这样的数组:

array(
    array('id' => 1),
    array('id' => 2),
)

Instead, i'd like to get an array like this:

相反,我想得到一个这样的数组:

array(
    1,
    2
)

How can I do that using Doctrine?

我如何使用 Doctrine 做到这一点?

回答by Lionel Gaillard

PHP < 5.5

PHP < 5.5

You can use array_map, and since you only have on item per array, you can elegantly use 'current'as callback, instead of writing a closure.

您可以使用array_map, 并且由于每个数组只有一个 item ,因此您可以优雅地 'current'用作回调,而不是编写闭包

$result = $em->createQuery("SELECT a.id FROM Auction a")->getScalarResult();
$ids = array_map('current', $result);

See Petr Sobotka's answer belowfor additional info regarding memory usage.

有关内存使用情况的其他信息,请参阅下面的 Petr Sobotka 的回答

PHP >= 5.5

PHP >= 5.5

As jcbwlkr's answered below, the recommended way it to use array_column.

正如jcbwlkr 在下面回答的那样,推荐使用array_column.

回答by jcbwlkr

As of PHP 5.5 you can use array_columnto solve this

从 PHP 5.5 开始,您可以使用array_column来解决这个问题

$result = $em->createQuery("SELECT a.id FROM Auction a")->getScalarResult();
$ids = array_column($result, "id");

回答by Ioan Badila

A better solution is to use PDO:FETCH_COLUMN. To do so you need a custom hydrator:

更好的解决方案是使用PDO:FETCH_COLUMN. 为此,您需要一个定制的水化器:

//MyProject/Hydrators/ColumnHydrator.php
namespace DoctrineExtensions\Hydrators\Mysql;

use Doctrine\ORM\Internal\Hydration\AbstractHydrator, PDO;

class ColumnHydrator extends AbstractHydrator
{
    protected function hydrateAllData()
    {
        return $this->_stmt->fetchAll(PDO::FETCH_COLUMN);
    }
}

Add it to Doctrine:

将其添加到 Doctrine:

$em->getConfiguration()->addCustomHydrationMode('COLUMN_HYDRATOR', 'MyProject\Hydrators\ColumnHydrator');

And you can use it like this:

你可以像这样使用它:

$em->createQuery("SELECT a.id FROM Auction a")->getResult("COLUMN_HYDRATOR");

More info: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes

更多信息:http: //docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes

回答by Petr Sobotka

Ascarius' answer is elegant, but beware of memory usage! array_map()creates a copy of passed array and effectively doubles memory usage. If you work with hundreds of thousands of array items this can become an issue. Since PHP 5.4 call-time pass by reference has been removed so you cannot do

Ascarius 的回答很优雅,但要注意内存使用!array_map()创建传递数组的副本并有效地将内存使用量加倍。如果您使用数十万个数组项,这可能会成为一个问题。由于 PHP 5.4 call-time pass by reference 已被删除,所以你不能这样做

// note the ampersand
$ids = array_map('current', &$result);

In that case you can go with obvious

在这种情况下,您可以使用明显的

$ids = array();
foreach($result as $item) {
  $ids[] = $item['id'];
}

回答by Minras

I think it's impossible in Doctrine. Just transform result array into the data structure you want using PHP:

我认为在 Doctrine 中这是不可能的。只需使用 PHP 将结果数组转换为您想要的数据结构:

$transform = function($item) {
    return $item['id'];
};
$result = array_map($transform, $em->createQuery("SELECT a.id FROM Auction a")->getScalarResult());