theoretic

Selectors and aggregation functions

Recommended Posts

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:

  1. 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.
  2. 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!

Share this post


Link to post
Share on other sites

You have two good answers here 

 

Soma's answer on Adrian's link does more or less what you propose in your last pseudo-code.

  • Like 2

Share this post


Link to post
Share on other sites
5 hours ago, diogo said:

Soma's answer on Adrian's link

Soma got one of the last Green Cards and moved from Switzerland to Atlanta? I must have missed a memo :lol:

  • Like 1

Share this post


Link to post
Share on other sites
On 2/14/2018 at 6:42 AM, theoretic said:

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?

for the most part, the hidden field option i think is best, but does take some logic and page saving; If you do want to have them be virtual, then you can do a hook

wire()->addHookProperty('Page(template=product)::rating', function($event) {
    $product = $event->object;
	$event->return = $product->points / $product->votes;
});

but you will be limited to in-memory selection, since the property is not in the database

  • Like 4

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By verdeandrea
      Hello girls and guys,
      i just saw that if I use a selector on a textarea, for example 
      $pages->find('body%=notes') it also find that key in html classes and attributes. For example it will find
      <p class="text-notes">Lorem ipsum dolor sit amet cosectetur.</p> Is there a way to avoid this anche check only the texts without the html tags?
      Thanks!
    • By activestate
      Hi,
      I'm having problems with seatching for a value in repeater.
      One of pages have attached repeater called 'insight_repeater'.
      This repeater has h1_tag (text field) and i'm trying to search for repeater items that contains 'lor' phrase.

      And right now i'm trying to do simple search, looking for phrase 'lor'. I've tried many approaches, but nothins seems to work, so i want to ask what i'm doing wrong here:

      <?php $query = $page->get("insight_repeater")->find('h1_tag~=Lor'); ?>

      Thanks for help.
       
    • By prestoav
      Hi all,
      I'm having an odd issue that I think is down to the selector I'm using but I can't track down the cause!
      I'm using front-end AJAX search form on a list of product adverts (pages)  where the product is made by a certain manufacturer (vendor). The manufacturer is chosen buy the person listing the adverts from a page field list of manufacturers who's parent is id=1034.
      Most manufacturers are being found without an issue. The only one that's causing a problem is called 'Proj-ect" (the "-" being part of their brand name.
      Here's my selector code:
      $thisBrandProducts = $pages->find("template=advert,vendor.title~=$q,has_parent=1034,limit=20,created_users_id!=$susDealerList"); $susDealerList is a list of advertisers that are not currently live so pages should be excluded.
      Could the "-" in the name be the issue with those pages not being returned? If so does anyone have any ideas of how I can get around this?
      Thanks in advance!
    • By theoretic
      Hello there! And thanks for Processwire!
      It may appear a noob question but what about selectors like "field1=val1||field2=val2"? I mean that the page we're looking for should have field1=val1 OR field2=val2 . There's plenty of examples for cases like "field=val1|val2" but it's another case.
      Practical example: frontpage uder registration. User should enter nick and email, and both of them should be unused by other registered users. It's logical to use something like
      $userTest = $users->get("nick={$input->post->nick}|email={$input->post->email}"); But this doesn't work.
      That's the best practice in this case? Thanks in advance!
    • By eangulo
      Hello,
      I passed through a lot of documentation about selectors and a few post in the forum but and I did not find my answer so I'll try here :).
      Question: Is there a way to find pages that have a certain field attached to his template ?
      Use case:
      // Site structure Products Computers Computer #1 (has field cached_details) Computer #2 (has field cached_details) Electronics Test #1 Pet Supplies Test #2 (has field cached_details but is empty) // If I want to get all children of page "products" that contains the field "cached_details" I would do something like this // $products => PageArray $results = $products->children("has_field=cached_details"); // or this $results = $products->children("cached_details!=null"); // Expected: $results would contains [Computer #1, Computer #2, Test #2] // Instead I have to do this $results = new PageArray(); foreach($products->children as $child) { if ($child->hasField("cached_details") === true) { $results->add($child); } }  
      Thank you in advance guys :),