Jump to content

Get unique parents from children and make them a page array


cb2004
 Share

Recommended Posts

I have been doing battle for the last couple of hours on this, I think I have got it but I just want to double check. Also I couldnt find anything on the forums so this could serve as help for other users.

I have a set of child pages that hold a date. I want to do a find on those children for those with date>=today and then spit out the parents to then handle further logic. I feel like I am missing something that ProcessWire can do here so please let me know if you know of a better way.

$results = $page->find("date_1>=today, sort=date_1, parent.status<" . Page::statusUnpublished);
foreach ($results as $p) $parents[] = $p->parentID;
$results = new PageArray();
$results->add($parents);

ProcessWire should handle the unique parents as the first $results may return 1001,1002,1001 for example.

Thanks as always people.

Link to comment
Share on other sites

The find call returns the pages in the order in which they are configured to be sorted. A PageArray keeps the order in which the pages were added (minus duplicates, for which the last addition counts) unless you explicitly call its sort method.

There is one exception to the rule, which is when you pass a PageArray to add() or append(). In that case, the items in that PageArray get prepended. I'm not sure if this is intentional.

  • Like 1
Link to comment
Share on other sites

Not sure, but I think it is intentional. But also, you always can (re)order / sort it after adding a complete PageArray at once:

http://cheatsheet.processwire.com/pagearray-wirearray/sorting-and-filtering/a-sort-property/

 

EDIT:
Ok, thats not my evening. :)

@BitPoet has already linked to PageArray->sort(), also to the newer API docs. Good night all together.

Edited by horst
  • Like 2
Link to comment
Share on other sites

$results = $pages->find("parent=$page->children, date_1>=today, parent.status<" . Page::statusHidden);
$parents = new PageArray();
foreach ($results as $p) $parents->append($p->parentID);

Append will get around the sort issue, cheers all.

Link to comment
Share on other sites

12 hours ago, cb2004 said:

$results = $pages->find("parent=$page->children, date_1>=today, parent.status<" . Page::statusHidden);
$parents = new PageArray();
foreach ($results as $p) $parents->append($p->parentID);

Append will get around the sort issue, cheers all.

This is the same as what @LostKobrakai showed in post 2: append() is identical to add(), and adding a page ID to a PageArray achieves the same as adding a Page object.

12 hours ago, horst said:

Isn't it, that you have an PageArray already in $result? Why not sorting this instead of the loop and double PageArray? Or do I get it wrong, what you are after?

The objective is to get the parents of the items in the first PageArray.

  • Like 1
Link to comment
Share on other sites

13 hours ago, Robin S said:

This is the same as what @LostKobrakai showed in post 2: append() is identical to add(), and adding a page ID to a PageArray achieves the same as adding a Page object.

The objective is to get the parents of the items in the first PageArray.

append() kept the order that the items were added to the array, add() kept the order that is setup within admin.

Link to comment
Share on other sites

4 hours ago, cb2004 said:

append() kept the order that the items were added to the array, add() kept the order that is setup within admin.

They really are the same thing:

Quote

Append an item to the end of the WireArray

This is a functionally identical alias of the WireArray::add() method here for naming consistency with the WireArray::prepend() method.

https://processwire.com/api/ref/wire-array/append/

https://github.com/processwire/processwire/blob/36984e4a057268b7a45b848e1b3b6ee757583459/wire/core/WireArray.php#L956-L959

  • Like 1
Link to comment
Share on other sites

So yeah, turns out add and append were exactly the same, must have been having a tough week as I thought my testing skills were ok :). I had to go with php array_unique to achieve what I wanted. I will post a code snippet in the morning.

Thanks all.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...