0

I want to sum a field tiempo_acumulado for each tarea_id. I try this but dont work, is wrong or how can do this.

foreach ($idstareas as $idtarea)
    {    
    $sumatorio = $this->ProyectosCategoriasTareas->find();
    $sumatorio
        ->select(['suma' => $sumatorio->func()->sum('tiempo_acumulado')])
        ->where(['tarea_id'=>$idtarea->id])
        ->toArray();
    debug($sumatorio);
    die();
    }

I dont find suma or the result

  • _doesn't work_ is not a proper problem description, it could mean pretty much anything. Please always try to describe what _exactly_ is happening (what results or errors do you get), and what _exactly_ you'd expect to happen instead (what results do you expect) - thanks! – ndm Apr 03 '20 at 11:25
  • When you do a `->toArray();` it returns a resultset which you need to store in a variable to be able to see the result. I prefer to write the toArray in separate statement to keep it clean. – Imdad Oct 08 '20 at 14:47

1 Answers1

0

This work! for me

foreach ($idstareas as $idtarea)
    {    
    $arrayTareas = $this->ProyectosCategoriasTareas->find('all')->where(['tarea_id'=>$idtarea->id]);
    $collection = new Collection($arrayTareas);
    $sumDeHoras = $collection->sumOf('tiempo_acumulado');
    debug($sumDeHoras);
    die();
    }