简体   繁体   中英

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?

THX for help :)

According to the documentation :

Query#getSingleResult(): Retrieves a single object. 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. 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(). 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

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