Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony 1.4 propel 1.6 : sum

I'm trying to get a sum of columns in propel. My code

$c = new Criteria();
$c->add(valuePeer::OWNER_ID, $this->getId());
$c->addSelectColumn('SUM(' . valuePeer::VALUE . ') as total');
$c->addGroupByColumn(valuePeer::VALUE);

$sum = valuePeer::DoSelect($c);

printing out $sum returns nothing (not even an empty object). all i get is Notice: Undefined offset: 1 in /.../lib/model/om/BaseValue.php on line 203 Notice: Undefined offset: 2 in /.../lib/model/om/BaseValue.php on line 204

I tried this approach with ::DoSelectRS($c) as suggested here and multiple other discussions but i get an error : Fatal error: Call to undefined method ValuePeer::DoSelectRS() in /.../lib/model/Restauracia.php on line 39.

Can anyone please tell me what is the right approach on this then?

like image 925
Kuro Avatar asked Dec 12 '22 22:12

Kuro


1 Answers

Why don't you use the new ModelCriteria instead of the old verbose one ?

$sum = ValueQuery::create()
  ->select(array('total'))
  ->filterByOwnerId($this->getId())
  ->withColumn('SUM(Value.Value)', 'total')
  ->find();

Will return something like:

PropelArrayCollection(
  array('total' => 25)
)
like image 64
j0k Avatar answered Dec 14 '22 11:12

j0k