Jump to content

Selector with a "+" operator


celfred
 Share

Recommended Posts

Hello,

I'm having a little question with this part of code :

  $rank = $pages->get("template=team, name=$selectedTeam")->rank->name;
  if ( $rank == '4emes' || $rank == '3emes' ) {
    $allPlayers = $pages->find("template=player, team.name=$selectedTeam");
    $allConcerned = new pageArray();
    $notConcerned = new pageArray();
    foreach($allPlayers as $p) { // Find players having at least 3 free elements
      if ($p->places->count()+$p->people->count() >= 3) {
        $allConcerned->add($p);
      } else {
        $notConcerned->add($p);
      }
    }
    $notConcerned = $notConcerned->implode(', ', '{title}');
    /* $allConcerned = $pages->find("template=player, team.name=$selectedTeam, (people.count+places.count>=3)"); // Find players having at least 3 places OR 3 people */
    /* $notConcerned = $pages->find("template=player, team.name=$selectedTeam, (places.count+people.count<3)")->implode(', ', '{title}'); */

It works fine the way it is, but I just want to make sure that the commented part is not possible with PW (yet?) : having a selector such as people.count+places.count>=3. I feel like it would be more concise and more readbable/maintainable (2 lines instead of 11).

Thanks in advance for your insight and advice ;)

Link to comment
Share on other sites

5 hours ago, celfred said:

/* $allConcerned = $pages->find("template=player, team.name=$selectedTeam, (people.count+places.count>=3)"); // Find players having at least 3 places OR 3 people */

I think you're looking for OR groups: https://processwire.com/api/selectors/#or-groups

$allConcerned = $pages->find("template=player, team.name=$selectedTeam, (people.count>=3), (places.count>=3)");

 

  • Like 2
Link to comment
Share on other sites

As Robin S recognized in your comment you are talking about OR, but your code is doing something else. This one should be able to replace your code (not tested)

$all = $pages->find("template=player, team.name=$selectedTeam");
$selector = "(people.count>=3), (places.count>=3),(people.count>=2, places.count>=1),(people.count>=1, places.count>=2)";
$allConcerned = $all->filter($selector);
$notConcerned = $all->not($selector);

As far as I know there is no diff function like array_diff for WireArrays.

  • Like 2
Link to comment
Share on other sites

Thanks for the replies !

I had tried the OR statement before (like Robin S mentionned), but I had an issue because of those players having 2 places and 1 people and not being concerned (or 1 place and 2 people), but Kixe showed me the way by adding all possibilities in the OR statement. I hadn't thought about this possibility.

Anyway, that would be more difficult if I wanted to pick players having more than a cumulated 10 number of free elements. I guess I would have to use my first 'foreach' solution in such a case.

Thanks again for your ideas !

Link to comment
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
 Share

×
×
  • Create New...