29 de agosto de 2011

Propel get average - mysql AVG

Here's how to do it:

public function getAverageRating()
  {
    $c = new Criteria;
    $c->addSelectColumn('avg(' . RatingPeer::RATING . ')');
    $c->add(RatingPeer::ID, $this->getId());
    
    $stmt = RatingPeer::doSelectStmt($c);    
    $average = $stmt->fetchAll(PDO::FETCH_COLUMN);
    if(array_key_exists(0, $average)) {
      return floatval($average[0]);
    }
    else
    {
      return 0;
    }
  }

Nenhum comentário:

Postar um comentário