Search the Community
Showing results for tags 'aggregation'.
-
Hi there! And thanks for Processwire! This is rather a question than a feature request, maybe there's a well known solution which i missed. So what about aggregation in selectors? Let's suppose we have a PW-powered shop where any product has a rating made by customers. Every time the user polls two data fields are updated: points and votes. The rating is calculated as this: $rating = $page->points / $page->votes; It was easy, yeah. But what about sorting products by their rating? Basically there are two ways of doing it: Creating one more field called rating and updating it after every vote. For sure it will work, but we have to create an extra data field which in fact stores data based on two another fields. Bad practice i think. Getting all products into array and applying a cutom sort function. Will work again, but this demands much more coding than using the native PW selectors. But how could it be done in the best possible way? Let's imagine this: $products = $page->children("sort=points/votes"); Or maybe this: function rating($a,$b){ return $a->points/$a->votes > $b->points/$b->votes; } $products = $page->children("sort=rating()"); Okay, let's go even deeper. Maybe we need "virtual" fields which are not stored in DB tables like other PW fields but represent the dynamically calculated values? $page->rating = function(){ return $page->points/$page->votes; } $products = $page->children("sort=rating"); Will be glad to see advice on the best practices. Thanks in advance!