biera
biera

Reputation: 2699

Propel and SQL aggregate functions

How does the following SQL query look like in Propel (1.4)?

SELECT SUM(`price_equipment`) FROM `order_equipment` WHERE `order_id` = 57072;

I did some research using google but all examples I found take into count GROUP BY clause. I just need a single value.

Upvotes: 1

Views: 3186

Answers (2)

Jordan Kasper
Jordan Kasper

Reputation: 13273

I think the syntax for Propel 1.4 would be something like:

$c = new Criteria();
$c->add(OrderEquipmentPeer::ORDER_ID, $orderId);
TestPeer::addSelectColumns($c);
$c->addAsColumn('price_sum', 'SUM('.OrderEquipmentPeer::PRICE_EQUIPMENT.')');
$results = OrderEquipmentPeer::doSelectStmt($c);

It's really hard to find documentation from 1.4 as they have killed the old wiki on propelorm.org, but I think that's right. As I said in my other comment though, you should consider upgrading to Propel 1.6 as it has a LOT more features and much better stability. The 1.4 branch is not being maintained at all as far as I know.

Upvotes: 1

DaOgre
DaOgre

Reputation: 2100

It's not going to hurt to add a group by statement here as it will help roll up your statement, just group by your orderId. If you don't want the group by, just drop that line.

$value = OrderEquipmentQuery::create()
          ->withColumn('SUM(price_equipment)')
          ->filterByOrderId(57072)
          ->groupByOrderId()
          ->find()

That's how you do it in 1.6, for 1.4 I would look at the example given by j0k, but keep in mind it's probably safe to group by orderId in your query

Upvotes: 6

Related Questions