Propel ORM: Calculate Average Column Count

I have a product object and I have rating objects that represent the product rating. A rating has a property called "value", which is an integer from 1 to 5.

For this product, I would like to get the average of all ratings. I know how to get all ratings:

$product->getRatingsRelatedByFromProductId(); 

But how can I get the average value of the entire rating

+4
source share
1 answer

The latest version (Propel 1.5) provides an example from a document: 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 assume that you can easily replace COUNT () with AVG (), MIN (), MAX () or any other aggregate function and delete ->groupBy() if you need to.

+3
source

All Articles