Home > database >  Return string from query Doctrine
Return string from query Doctrine

Time:10-24

I have a query that sums up the total of fields in my database.

//HoursRepository.php

public function findHoursTotal($user)
{
    return $this->createQueryBuilder('h')
        ->where('h.user = :user')
        ->select('SUM(h.total)')
        ->setParameter('user', $user)
        ->getQuery()
        ->getResult();
}

This query returns:

array:1 [▼
  0 => array:1 [▼
    1 => "52400"
  ]
]

My question is how do i get just the "52400" result as a string and not in an array?

CodePudding user response:

you should use getSingleScalarResult(), bellow the usage example :

return $this->createQueryBuilder('h')
        ->where('h.user = :user')
        ->select('SUM(h.total)')
        ->setParameter('user', $user)
        ->getQuery()
        ->getSingleScalarResult();
  • Related