Jump to content
celfred

Same Find request with different results ?

Recommended Posts

Hello !

I have somehting I don't understand here... Here's my code :

$allPlayers = $pages->find("parent.name=players, team=$selectedTeam");
$allTrains = $allPlayers->find("template=event, task.name~=ut-action, refPage!='', date>=$startDate, date<=$endDate, sort=refPage, sort=date");
bd('$allTrains:'.$allTrains->count()); // DISPLAYS 0 ????
foreach($allPlayers as $p) {
  $allTrainings = $p->find("template=event, task.name~=ut-action, refPage!='', date>=$startDate, date<=$endDate, sort=refPage, sort=date");
  $test += $allTrainings->count();
}
bd('$test:'.$test); // DISPLAYS 883 pages (normal)
	

As you can read from my comments, I have no idea why my first $allTrains stays at 0 while the second request actually finds the corresponding pages. If someone could explain I'd appreciate a lot. I have been struggling with this for hours now... For your information, my pages having template 'event' are in a subtree like so :

- player 01

  - history-1

    - event 01

   - event 02

   - event ...

  - history-2

    - event 01

    - event ...

- player 02

  - history-1

    - event 01

   - event 02

   - event ...

  - history-2

    - event 01

    - event ...

- player ...

  - history-1

    - event 01

   - event 02

   - event ...

  - history-2

    - event 01

    - event ...

 

Thanks in advance. (sorry for my preceding 'tree' which doesn't look like much. I need to find a way to output this better 😉 )

Edited by celfred
Code was on one line.

Share this post


Link to post
Share on other sites

I think I am not wrong saying that actually its not the same request.  On the first time you are calling find() on a PageArray and in the second time, find() on a Page object.

 

1 hour ago, celfred said:

$allPlayers = $pages->find("parent.name=players, team=$selectedTeam");

$allPlayers is now a PageArray and contain only pages with the ~history~ template.

When you call find() on a PageArray, you are searching for pages matching your selector that are actually in the PageArray (their children are not in this array!) and that why you get a result of 0 page found.

Try to call $allPlayers->children("template=event") on the PageArray to get an imploded IDs string of all the children of the pages that are in the PageArray.

 

1 hour ago, celfred said:

$allTrainings = $p->find("template=event, task.name~=ut-action, refPage!='', date>=$startDate, date<=$endDate, sort=refPage, sort=date");

Here you are calling find() on a Page object. When you call find() on a Page object, you are searching Pages anywhere below this page object (children, grandchildren, etc.) that are matching the selector.

 

  • Thanks 1

Share this post


Link to post
Share on other sites

I'm starting understanding things better 😉 Thanks a lot for this helpul answer !

I'll keep exploring to build a better knowledge of what I'm doing exactly.

Share this post


Link to post
Share on other sites

Hey,

Sorry to come back on this, but I'm still struggling...

Why is this working as expected ?

$notTeacherActions = $pages->find("template=task, owner.singleTeacher=$user"); // Returns 2 pages having the $user in the owner repeater singleTeacher field

Whereas this is not :

$notTeacherActions = $pages->find("template=task, owner.singleTeacher!=$user"); // Returns nothing where it should return all tasks NOT having this particular user in the singleTeacher owner repeater ? 

I have a feeling this has to do with my misunderstanding in the previous posts... So I continue here 🙂

By the way, still in this issue, I'm stuck with :

$globalActions = $pages->get("name=tasks")->children(); // I have 62 tasks pages
$adminActions = $globalActions->find("adminOnly=1"); // I have 10 adminActions
$allActions = $globalActions->find("adminOnly=0"); // I have 45 non admin (available to the logged-in user
bd($allActions->count()); // This shows me the 45

// The following are my tests to understand... Which I don't :(
// First tests from $pages
$teacherActions = $pages->get("name=tasks")->children("owner.singleTeacher=$user"); // This returns 2
$notTeacherActions = $pages->find("template=task, owner.singleTeacher!=$user"); // This returns nothing...
$notTeacherActions = $pages->get("name=tasks")->children("adminOnly=0")->not("owner.singleTeacher=$user")->sort("category.title"); // This actually returns 45 ($allActions, including the 2 returned in $teacherActions...)
bd('from $pages:'.$teacherActions->count().'/'.$notTeacherActions->count()); // I get 2 and 45

// Second test from $allActions
// THIS SEEMS LINKED TO MY ORIGINAL REQUEST (AND I'M STILL LOST !)
$teacherActions = $allActions->find("owner.singleTeacher=$user");
$notTeacherActions = $allActions->not("owner.singleTeacher=$user")->sort("category.title");
bd('from $allActions:'.$teacherActions->count().'/'.$notTeacherActions->count()); // Returns 0 and 45

As you can see, I still have a long way to go tu understand all this... Sorry if I'm being hard to follow, but I am indeed confused on this issue...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By schwarzdesign
      ProcessWire added the numReferences property in 3.0.107, which returns the count of all pages having a reference to the page object. However, apparently it's not possible to use this property inside a selector. I'm trying to find all pages that are referenced at least once:
      $pages->find('template=service, numReferences>0'); This throws an error: "Field does not exist: numReferences".
      Is there another way to filter by the number of references? Of course, I could manually filter the results of the find query, but that feels overly complicated. Ideally, I would also like to filter the number of references from a specific field; that is, find all pages that are referenced at least once in one specific page reference field.
      Is there a way to do this? I guess supporting numReferences in selectors would be a feature request - if so, is it feasible?
      Thanks!
       
    • By louisstephens
      From my last post, I was given a good idea on how to count the repeater items, and it worked wonderfully. I got my code working well and the columns (based on the count) all work well as well. Now, I have a head scratcher on my hands. 
      <?php $buttonsIncluded = $page->special_custom_buttons->find('special_custom_buttons_include=1'); $buttonsIncludedCount = count($buttonsIncluded); $buttonsIncludedCountAdditional = $buttonsIncludedCount +1; echo $buttonsIncludedCount; ?> <div class="row"> <?php foreach($buttonsIncluded as $button): ?> <?php if($button->custom_buttons_include): ?> <?php if($buttonsIncludedCountAdditional == 2): ?> <div class="col-6"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 3): ?> <div class="col-4"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 4): ?> <div class="col-3"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php endif; ?> <?php endif; ?> <?php endforeach; ?> </div> All of this is included in a larger foreach statement that is pulling in other data (like body copy etc etc) from a Page Table field. As you can see in my code above, I am adding "1" to the count, so I can have space in the grid layout for a new button.
      So, right now: it looks something like: 
      [repeater button] [repeater button] [repeater button] [space for new button] What I really need to do is to pull in the button from the Page Table and add it into the new space so it looks like:
      [repeater button] [repeater button] [repeater button] [button from Page Table] Is this even possible todo, or is there a better way to go about this? 
       
      *Edit*
      So, I really just overlooked something quite easy here. Since the grid is based on 12 columns, I could just take 12 and divide by $buttonsIncludedCountAdditional which would give me the remaining col width to use outside the foreach loop. I was trying to make this too complicated.
    • By pwFoo
      I played with the Selectors object and would like to use it for a special use case...
      That is a custom Selectors object with dummy data converted to an php array to see the structure (Selectors object is a WireArray with "fields" added)
      Array ( [0] => Array ( [0] => Array ( [field] => seg1 [value] => val1 [not] => [group] => [quote] => [forceMatch] => ) [1] => Array ( [field] => seg2 [value] => val2 [not] => [group] => [quote] => [forceMatch] => ) ) )  
      But instead of "seg1" the field name is "field" with value "seg1" (= my field name).
       
      So I can't search the Selectors WireArray (= custom WireArray with added Selectors objects) with PW "find('seg1=val1')", Is there a way to search with "find()" or build a simple wrapper to make the elements searchable / filterable with find()?
    • By celfred
      Hello,
      I'm struggling with this : a 'group' field of 'Page' type.
      The parent of selectable pages is '/groups', the template is 'group', and a member can create some groups in the /groups tree. I would like this user to see only the groups he or she has created so in my 'Find selector' in the backend, I would like to use :

      template=group, created_users_id=$user->id But that doesn't seem to work... I still get the list of all available groups in the /groups tree.
      Any idea ?
      I had a feeling I had already seen that before (something like $user->id must be replaced by users_id or something, but I can't find anything in the Forums... and all my tests keep failing...
    • By rolisx
      Hi all
      I have a problem here. I created a gallery with 240 pictures. Created an images field with no maximum amount (0). Unfortunately, only 98 of the 240 images show on the website. Any idea what I possibly could have done wrong? Thanks for your help!
      <?php foreach ($page->images as $image) { $options = array( 'quality' => 90, 'upscaling' => false ); $thumb = $image->size(250, 250, $options); ?> <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 col-6 foto"> <a href="<?= $image->url ?>" data-lightbox="lightbox" > <img src="<?= $thumb->url ?>" alt="" > </a> </div> <?php } ?>  
×
×
  • Create New...