4

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

Pascal Klein
  • 23,665
  • 24
  • 82
  • 119

1 Answers1

3

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.

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
  • Thanks. I find it kind a weired though, that the Count is supported with a simple method "$authors->getBooksRelatedByAuthorId()->count(); but other aggregate functions are not... – Pascal Klein Dec 08 '10 at 13:27