Home > Symfony Framework > How to do SUM with Propel

How to do SUM with Propel

February 6th, 2009

http://www.digitalbase.eu/blog/simplifying-queries-using-sum-count-etc-in-propel/

$c->addSelectColumn(’sum(‘.dbTimeBlockPeer::TOTAL.’) as total’);
$rs = dbTimeBlockPeer::doSelectRS($c);
while ($rs->next()) { $total = $rs->getInt(1); }

And a real world example from symfony users mailing list (Symfony 1.2):

ACTION:

$cu = new Criteria();
$cu->clearSelectColumns();
$cu->add(DokumentLinikiPeer::DOKUMENT_ID, $this->getUser()->getAttribute(‘dokument_id’));
$cu->addSelectColumn(DokumentLinikiPeer::STAWKI_VAT);

$cu->addSelectColumn(’sum(‘.DokumentLinikiPeer::KWOTA_NETTO.’) as
SumOfNetto’);

$cu->addSelectColumn(’sum(‘.DokumentLinikiPeer::KWOTA_BRUTTO.’) as
SumOfBrutto’);

$cu->addGroupByColumn(DokumentLinikiPeer::STAWKI_VAT);

$sumy = DokumentLinikiPeer::doSelectStmt($cu);

$total = array();

while ($row = $sumy->fetch(PDO::FETCH_NUM)) {

$total[] = array(
‘vat’ => StawkiVatPeer::retrieveByPK($row[0]), // VAT
‘netto’ => $row[1], // SumOfNetto
‘brutto’ => $row[2], // SumOfBrutto
);

}

TEMPLATE:
<?php foreach($sumy as $suma): ?>
<tr>
<td><?php echo $suma['vat']; ?></td>
<td><?php echo $suma['netto']; ?></td>
<td><?php echo $suma['brutto']; ?></td>
</tr>
<?php endforeach ?>

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Symfony Framework ,

  1. April 29th, 2009 at 14:34 | #1

    Alternatively,

    $count = MyPeer::doCount(new Criteria());

  2. April 29th, 2009 at 14:34 | #2

    Oops, just realised yours was for SUM not COUNT :-)

  1. No trackbacks yet.
Subscribe to comments feed