更改主键值的getResult数组键

时间:2020-12-24 21:30:41

Is it possible to change the array key values for the getResult() in Doctrine2?

是否可以在Doctrine2中更改getResult()的数组键值?

Example:

$qb->select('t.id, t.name')->from('Table', 't');

$ qb-> select('t.id,t.name') - > from('Table','t');

When I print this, I get, which is not what I wanted:

当我打印这个时,我得到了,这不是我想要的:

print_r($qb->getQuery()->getResult());

//Print result: Array ( [0] => Array ( [id] => 20 [name] => Name1 ) [1] => Array ( [id] => 21 [percentagem] => Name2 ) )

的print_r($ QB-> getQuery() - >的getResult()); //打印结果:数组([0] =>数组([id] => 20 [名称] =>名称1)[1] =>数组([id] => 21 [百分比] =>名称2))

What I want is:

我想要的是:

Array ( [20] => Array ( [id] => 20 [name] => Name1 ) [21] => Array ( [id] => 21 [percentagem] => Name2 ) )

数组([20] =>数组([id] => 20 [名称] =>名称1)[21] =>数组([id] => 21 [百分比] =>名称2))

Suggestions, hints would be appreciated.

建议,提示将不胜感激。

3 个解决方案

#1


31  

I'm actually very happy about how cool this thing is:

我真的很高兴这件事有多酷:

$query = $this->getEntityManager()->createQuery('
            SELECT user FROM UserBundle:User user
            INDEX BY user.id
            WHERE user.id = 1
            '
        );

The INDEX BY construct is nothing that directly translates into SQL but that affects object and array hydration. After each FROM and JOIN clause you specify by which field this class should be indexed in the result. By default a result is incremented by numerical keys starting with 0. However with INDEX BY you can specify any other column to be the key of your result, it really only makes sense with primary or unique fields though.

INDEX BY构造不会直接转换为SQL,但会影响对象和数组的水合作用。在每个FROM和JOIN子句之后,您可以指定此类应在结果中编入索引的字段。默认情况下,结果会以从0开始的数字键递增。但是使用INDEX BY,您可以指定任何其他列作为结果的键,但实际上只对主要字段或唯一字段有意义。

Source: Doctrine ORM 2 Documentation Using INDEX BY

来源:使用INDEX BY的Doctrine ORM 2文档

  • Please use INDEX BY prior to WHERE
  • 请在WHERE之前使用INDEX BY

#2


15  

However, for the sake of completeness, you can do the same with the query builder as shown below:

但是,为了完整起见,您可以对查询构建器执行相同操作,如下所示:

$queryBuilder = $this->getEntityManager()->createQueryBuilder();

$queryBuilder
    ->select('user')
    ->from('UserBundle:User', 'user', 'user.id')
    ->where('user.id = :userId')
    ->setParameter('userId', $userId)
;

var_dump(
    $queryBuilder->getQuery()->getArrayResult()
);

As you can see the index by option is available as the third parameter of the query builder from method:

正如您所看到的,index by option可用作方法中查询构建器的第三个参数:

/**
 * Creates and adds a query root corresponding to the entity identified by the given alias,
 * forming a cartesian product with any existing query roots.
 *
 * <code>
 *     $qb = $em->createQueryBuilder()
 *         ->select('u')
 *         ->from('User', 'u')
 * </code>
 *
 * @param string $from    The class name.
 * @param string $alias   The alias of the class.
 * @param string $indexBy The index for the from.
 *
 * @return QueryBuilder This QueryBuilder instance.
 */
public function from($from, $alias, $indexBy = null)
{
    return $this->add('from', new Expr\From($from, $alias, $indexBy), true);
}

#3


0  

Just use the 3-rd parameter of ->from(entity, alias, indexBy)

只需使用 - > from(entity,alias,indexBy)的第3个参数

So, instead

$qb->select('t.id, t.name')->from('Table', 't');

use

$qb->select('t.id, t.name')->from('Table', 't', 'Table.id');

PS: @Francesco-Casula wrote good answer with little more details, well.

PS:@ Francesco-Casula用更多的细节写出了很好的答案。

#1


31  

I'm actually very happy about how cool this thing is:

我真的很高兴这件事有多酷:

$query = $this->getEntityManager()->createQuery('
            SELECT user FROM UserBundle:User user
            INDEX BY user.id
            WHERE user.id = 1
            '
        );

The INDEX BY construct is nothing that directly translates into SQL but that affects object and array hydration. After each FROM and JOIN clause you specify by which field this class should be indexed in the result. By default a result is incremented by numerical keys starting with 0. However with INDEX BY you can specify any other column to be the key of your result, it really only makes sense with primary or unique fields though.

INDEX BY构造不会直接转换为SQL,但会影响对象和数组的水合作用。在每个FROM和JOIN子句之后,您可以指定此类应在结果中编入索引的字段。默认情况下,结果会以从0开始的数字键递增。但是使用INDEX BY,您可以指定任何其他列作为结果的键,但实际上只对主要字段或唯一字段有意义。

Source: Doctrine ORM 2 Documentation Using INDEX BY

来源:使用INDEX BY的Doctrine ORM 2文档

  • Please use INDEX BY prior to WHERE
  • 请在WHERE之前使用INDEX BY

#2


15  

However, for the sake of completeness, you can do the same with the query builder as shown below:

但是,为了完整起见,您可以对查询构建器执行相同操作,如下所示:

$queryBuilder = $this->getEntityManager()->createQueryBuilder();

$queryBuilder
    ->select('user')
    ->from('UserBundle:User', 'user', 'user.id')
    ->where('user.id = :userId')
    ->setParameter('userId', $userId)
;

var_dump(
    $queryBuilder->getQuery()->getArrayResult()
);

As you can see the index by option is available as the third parameter of the query builder from method:

正如您所看到的,index by option可用作方法中查询构建器的第三个参数:

/**
 * Creates and adds a query root corresponding to the entity identified by the given alias,
 * forming a cartesian product with any existing query roots.
 *
 * <code>
 *     $qb = $em->createQueryBuilder()
 *         ->select('u')
 *         ->from('User', 'u')
 * </code>
 *
 * @param string $from    The class name.
 * @param string $alias   The alias of the class.
 * @param string $indexBy The index for the from.
 *
 * @return QueryBuilder This QueryBuilder instance.
 */
public function from($from, $alias, $indexBy = null)
{
    return $this->add('from', new Expr\From($from, $alias, $indexBy), true);
}

#3


0  

Just use the 3-rd parameter of ->from(entity, alias, indexBy)

只需使用 - > from(entity,alias,indexBy)的第3个参数

So, instead

$qb->select('t.id, t.name')->from('Table', 't');

use

$qb->select('t.id, t.name')->from('Table', 't', 'Table.id');

PS: @Francesco-Casula wrote good answer with little more details, well.

PS:@ Francesco-Casula用更多的细节写出了很好的答案。