Example #1
0
 /**
  * Get average value for field of records in database founded by sql-condition
  *
  * @param  string	$field_name
  * @param  string	$condition
  * @return float
  */
 public function dbAverage($field, $condition = null)
 {
     $sql = $this->getModel()->_getSelectQuery_(null, $condition);
     $sql = preg_replace("'select .* from 'si", "select avg(" . $this->getTableName() . "." . $field . ") as average from ", $sql);
     if ($this->_SQL_DEBUG) {
         die($sql);
     }
     $res = $this->_db->execSql($sql);
     return is_array($res) ? $res[0]['average'] : null;
 }