Last active
November 3, 2021 10:35
-
-
Save elvismdev/5447ac515bcc776c3bff2fbfe936f560 to your computer and use it in GitHub Desktop.
SUM up all values from table column - Doctrine QueryBuilder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Sum all values from order_total column in completed orders, and return. | |
$qb = $em->createQueryBuilder(); | |
$qb = $qb | |
->select( 'SUM(e.orderTotal) as totalRevenue' ) | |
->from( '[Vendor]\[Package]\Entity\Orders', 'e' ) | |
->where( $qb->expr()->andX( | |
$qb->expr()->eq( 'e.orderStatus', ':status' ), | |
// $qb->expr()->in( 'e.state', array( 'queued', 'confirmed' ) ) | |
) ) | |
->setParameter( 'status', 'completed' ) | |
->getQuery() | |
; | |
$totalRevenue = $qb->getOneOrNullResult(); | |
print_r( $totalRevenue ); |
Thx you !
A slightly easier syntax would be:
public function getAmountToBeInvoiced()
{
return $this->createQueryBuilder('t')
->select('(sum(t.quantity * t.amount)/100) as invoicableAmount')
->andWhere('t.invoicable = :true')
->andWhere('t.invoiced = :false')
->setParameter('true', true)
->setParameter('false', false)
->distinct()
->getQuery()
->getOneOrNullResult();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!