1

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
biera
  • 2,608
  • 1
  • 24
  • 26
  • Take a look at this question: [symfony 1.4 propel 1.6 : sum](http://stackoverflow.com/questions/12308008/symfony-1-4-propel-1-6-sum) – j0k Jan 24 '13 at 14:34

2 Answers2

6

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

DaOgre
  • 2,080
  • 16
  • 25
  • Note that this answer is still for 1.6 (not Propel 1.4 as OP specified), but really, @biera, you should consider upgrading to 1.6 - it is significantly better. – Jordan Kasper Jan 24 '13 at 20:20
1

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.

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55