I wrote mysql query that needs to collect the SUM of amount field by selected user(in this table, user_id foreign key to Users table).
It look like this and it works..
SELECT SUM(amount) AS total_sum, user_id FROM user_donation Group by user_id
I have trouble writing it in symfony to do the exact same thing. I can't figure out what am I doing wrong..
My code..
public function getTotalDonated($userId)
{
$user = $this->getUserRepository()->find($userId);
$query = $this->getDonationRepository()
->createQueryBuilder('g')
->select("SUM(amount) AS total_sum, user")
->groupBy('g.user')
->setParameter(['user' => $user])
->getQuery();
return $query;
}
$query = $this->getDonationRepository()
->createQueryBuilder('g')
->select("SUM(g.amount) AS total_sum")
->where('g.user = :user')
->groupBy('g.user')
->setParameter('user', $user)
->getQuery()
->getResult();
return $query;
I found the solution!
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.