Zend Framework echoes the value of a SUM request

I created a query for the zend framework in which I am trying to get the sum of a column, in this case a column called "time". This is the query I'm using:

$this->timequery = $this->db_tasks->fetchAll($this->db_tasks->select()->from('tasks', 'SUM(time)')->where('projectnumber =' . $this->value_project));


$this->view->sumtime = $this->timequery;

Repeating the request tells me that is correct. But I can not answer the result correctly. I am currently using:

echo $this->sumtime['SUM(time)'];

Return the following error:

Catchable fatal error: Object of class Zend_Db_Table_Row could not be converted to string in C:\xampp\htdocs\BManagement\application\views\scripts\tasks\index.phtml  on line 46

Line 46 is an echoed line in my view.

I’ve been looking for how to understand this for two days, or am getting the same result in a different way. Tried to serialize the value, but that didn't work either.

Is there anyone who knows how to achieve the total amount of a database column?

Any help is very convenient!

note: Quite new for zend framework ...

+3
3

Zend_Db , fetchAll ( ) :

  • fetchAll -
  • fetchRow -
  • fetchOne -

:

$sum = $db->fetchOne('SELECT SUM(time) FROM tasks WHERE project_number = ?', $this->value_project);

Zend_Db_Select , , :

//note that the SUM(time) part is passed as a Zend_Db expression, as it is not a column
$select = $this->db_tasks->select()
                         ->from('tasks', new Zend_Db_Expr('SUM(time)'))
                         ->where('projectnumber =' . $this->value_project);

$this->timequery = $this->db_tasks->fetchOne($select);

, Zend_Db_Select toString SQL.

+9
$timequery= $timequeriestb->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                          ->columns('SUM(time) AS Totaltime')
                          ->where('projectnumber ='. $this->value_project));
+1

The SQL you want is probably:

SELECT SUM(time) AS time_sum FROM tasks ...

I don’t know how to do this in Zend. Then:

echo $this->sumtime['time_sum'];
0
source

Source: https://habr.com/ru/post/1741244/


All Articles