Propel ORM: Calculating Average of a column

average, orm, propel, sql

Solution

For latest version (Propel 1.5), here is an example from the doc : http://www.propelorm.org/wiki/Documentation/1.5/ModelCriteria#AddingColumns

$authors = AuthorQuery::create()
  ->join('Author.Book')
  ->withColumn('COUNT(Book.Id)', 'NbBooks')
  ->groupBy('Author.Id')
  ->find();
foreach ($authors as $author) {
        echo $author->getName() . ': ' . $author->getNbBooks() . " books\n";
}

I guess that you can easily replace COUNT() by AVG(), MIN(), MAX() or any other aggregate function, and remove the `->groupBy()` if you need.

Problem

I have a product object and I have rating Objects, which represent a rating about a product. A rating has a property named "value", which is an Integer from 1 to 5. For a given product I would like to get the average of the values of all ratings. I know how to get all ratings: ``` $product->getRatingsRelatedByFromProductId(); ``` But how can I get the average of the values of all rating

Original source