简体   繁体   English

Doctrine 2 DQL + getRow作为数组

[英]Doctrine 2 DQL + getRow as array

i have a really simple query: 我有一个非常简单的查询:

$query = $this->em->createQuery('SELECT u FROM UserBundle:User u WHERE u.username = ?1');
$query->setParameter(1, $username);

Right now i fetch my result normal as an array: 现在,我以数组形式正常获取结果:

$data = $query->getArrayResult();
$user = $data[0];

Is there a way to get only one row as array directly without doing the $data[0] step? 有没有一种方法可以直接获取仅一行作为数组,而无需执行$ data [0]步骤?

THX for help :) THX寻求帮助:)

According to the documentation : 根据文档

Query#getSingleResult(): Retrieves a single object. Query#getSingleResult():检索单个对象。 If the result contains more than one or no object, an exception is thrown. 如果结果包含多个对象或不包含任何对象,则将引发异常。 The pure/mixed distinction does not apply. 纯/混合区别不适用。

Query#getSingleScalarResult(): Retrieves a single scalar value from the result returned by the dbms. Query#getSingleScalarResult():从dbms返回的结果中检索单个标量值。 If the result contains more than a single scalar value, an exception is thrown. 如果结果包含多个标量值,则抛出异常。 The pure/mixed distinction does not apply. 纯/混合区别不适用。

You might use getSingleResult() instead of getArrayResult(). 您可以使用getSingleResult()代替getArrayResult()。 It's documented in this chapter of the doctrine doc: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#executing-a-query 在文档文档的这一章中对此进行了记录: http : //docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#executing-a-query

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

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