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]);
return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>