Jump to content

Confused with different results after pages->find()


Recommended Posts

Hello,

It's been a while since I've posted here, but I still exist and my PW site is still being used by my pupils.

Trying to improve things due to long oading pages, I'm struggling with an issue I don't understand.

This part of code returns appropriate results : $listNegAttitude shows dates if $negAttitude has elements)

$player->negAttitude = $pages->find("has_parent=$player->id,template=event,date>=$dateStart, date<=$dateEnd, task.category.name=attitude, task.HP<0");

[…]
if ($player->negAttitude->count() > 0)
{
 $listNegAttitude = $player->negAttitude->implode(function ($item) {return "- ".date('d/m', $item->date)."<br />"; });
 bd($listNegAttitude);
}

Whereas this one triggers the bd() because $negAttitude has elements, BUT SHOWS AN EMPTY STRING :

 
$allEvents = $pages->findMany("has_parent=$player->id,template=event,date>=$dateStart, date<=$dateEnd");
$player->negAttitude = $allEvents->find("task.category.name=attitude, task.HP<0");

[…]

if ($player->negAttitude->count() > 0)
{
 $listNegAttitude = $player->negAttitude->implode(function ($item) {return "- ".date('d/m', $item->date)."<br />"; });
 bd($listNegAttitude);
}

I don't understand.

My point was to make 1 'big' request with the findMany (to gather all events recorded for 1 player) and then wotk from this big pageArray to find out about categories of events (to display a report about a player). The 'big' pagearray is about 500 pages at the most, and it loops about 30 times (for 30 players).

Thanks if anyone can help me understand this issue.

I've posted in the "Getting started" forum because I have a feeling this is a basic issue.

Link to comment
Share on other sites

Hi,

not sure i'm right but my first thought is that your first $pages->findMany returns an array and if you want to sort the result afterwards you should use an foreach on $allEvents and not the ->find method

just an idea 🙂

have a nice day

Link to comment
Share on other sites

Thanks a lot @virtualgadjofor your answer.

I kind of get it, but I'm still wondering :

  • Tracy debugger gives me Processwire/Pagearray for all my variables.
  • the ->find() in case #2 seems to work since the following  $player->negAttitude->count() returns something, but nothing works on it (ie my implode() returns empty strings (except in case #1, which I guess shows that what you say is right. In case #2, it works if I loop on $player->negAttitude with a for loop (but not a foreach() ???) :

This works (in case #2) :

for ($i = 0; $i < $player->negAttitude->count(); $i++) 
{                                                                                    
  $listNegAttitude .= '- '.date("d/m", $e->date).': ['.$e->task->title.'] '.$e->summary.'<br />'; 
}                                                                                                   

This doesn't :

foreach ($player->posAttitude as $e)
{
 $listNegAttitude .= '- '.date("d/m", $e->date).': ['.$e->task->title.'] '.$e->summary.'<br />';
}

I still don't get it… but I'm sure there must be an explanation.

Link to comment
Share on other sites

Hi again,

actually it's sooner in the code that i would have siwtched to array functions like foreach, map and so so on, more something like

$allEvents = $pages->findMany("has_parent=$player->id,template=event,date>=$dateStart, date<=$dateEnd");
foreach($allEvent as $ae){
    if(...) { // here come your conditions like $e->template == '... ' $ae-date>= and so on
        $player->negAttitude = ... // and here trhe details/pages... you store
    }
}

the case you descibe is normal, allEvent (result of a pages find is an array of pages and as much as i love working with objects, php is reallt fast and strong with arrays and in your code you switch from object ($pages) to array ->find() result) and then again to object methods ($player->negAttitude = $allEvents->find...), i can try and guess why foreach may become a little reluctant to eat what you give 🙂
honestly, what i do in this kind of situation is to dump/print_r the result step by step to see exactly what i get and then which will be the best method to use and parse it and of course, once i've switched from object to array i try to stay with it to avoid surprises 🙂

have a nice day

 

  • Like 1
Link to comment
Share on other sites

Thanks a lot for such great explanations !

I had no idea I was switching between object and array… I'll do more testing keeping that in mind and double-checking my code everywhere because I have a feeling this must happen in many places on my site…

My point was to avoid doing many $pages->find requests, but then, ignoring this array/object difference, I kept using the ->find() way of doing things (which I appreciate a lot with PW).

Thanks again for your help and have a nice day as well !

Link to comment
Share on other sites

Hi,

my pleasure of course 🙂

and you know this travel between object and arrays is somehow natural when using pw api
everything is an object, pages, images, files, ... but, when using ->find, ->findMamy, it returns an array... of objects 😄 that's why when looping through the result arrays function/method are the good ones but, inside the loop, each element you get is... an object, thus the -> syntax, same thing actually as a repeater
afterwards, the nature of what you're feeding is up to you, array, object, string, open bar...

have a nice pw day 🙂

  • Like 1
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...