Blue Horn

Symfony Framework

How to do SUM with Propel

February 6, 2009 by Sid in Symfony Framework with 2 Comments

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 ?>

Tagged ,

Related Posts

2 Comments

  1. JevonApril 29, 2009 at 2:34 pm

    Alternatively,

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

  2. JevonApril 29, 2009 at 2:34 pm

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

Leave a reply

Your email address will not be published. Required fields are marked *

*

10 − 9 =

My Projects
Restaurant Websites
Websites