Jump to content

find() on my own little PageArray


thomas
 Share

Recommended Posts

Hello,

I have a function that creates a PageArray, throws a bunch of pages into it and returns it. Now I'd like to use all the PageArray methids on it but for some reason, everything after the first selector is ignored.

The function:

function getRelated($pageTags,$template,$limit=1000) {
 $pages = wire('pages');
 foreach($pageTags as $tags) {
   $tag = wire('sanitizer')->selectorValue($tags->title);
   if($template) $templateQ = ",template=$template";
   $related_pages = $pages->find("tags.title=$tag,id!=".wire('page')->id.$templateQ);
   foreach($related_pages as $related) {
     $related_id[$related->id]++;
   }
 }
 $rp = new PageArray();
 foreach($related_id as $key => $val) if($tmp++ < $limit) {
   $rel_page = $pages->get($key);
   $rel_page->rank = $related_id[$key];
   $rp->add($rel_page);
 }
 return $rp;
}

How I'd like to use it:

$rel = getRelated($page->tags);
$start = $input->urlSegment1 | 0;
$related = $rel->find("sort=-rank,sort=-created,start=$start,limit=3");

This returns all the elements in $rel, sorted by rank, everything else is ignored.

Is this a bug I built or a bug I found?

Thanks,

thomas

Edited by Soma
added intendation
Link to comment
Share on other sites

I just wrote one answer but erased everything before posting it as I found almost everything in it dead wrong. Another try.

And then I started to write another explanation, but after checking if I'm reading the latest code decided to give this even one more try. Maybe I shouldn't have tried to answer this now :).

And here's the answer, finally: you're right in the sense that there actually is a bug in the code you're running. But this issue has been fixed by Ryan a month ago, so updating to the latest version from GitHub should fix your problem. I was reading the old version myself at first too...

Edit: I did have something right in the first version of my answer. Here goes, again.

Only one sort selector is supported at the moment, not even "sort=-rank,-created" works. There's a comment in the code saying "Currently only sorts by one field at a time".

And find() for arrays does not support "start" at all, but this can be achieved by leaving "limit" (and "start") from the selector and calling $array->slice($start, $limit) instead for the sorted array.

Edited by nik
  • Like 1
Link to comment
Share on other sites

You can do this though I think:

$pages->find('template=something,sort=-id')->sort('-title');

That selector is nonsense of course, but I'm sure you can do this to sort by two things.

Link to comment
Share on other sites

I also thought about this Pete but it doesn't sort by multiple. It sorts first all by id and then all by title.

Well since I'm also in need of such exact feature for my project, I took this approach and created a little helper module. After install you have a new method on $pages var. $pages->findRelated(...);

$pages->findRelated( Page $page, PageArray $tags, string $field [, string $tmpl, int $limit] );

I tried also to do it with the PW API only but it isn't possible to sort by score AND modified so I commented that part out and wrote a plain sql query to do it instead.

To use it you do something like this:

$found = $pages->findRelated($page, $page->tags, 'tags', 'product|product2', 50);
if($found) {
 foreach( $found as $rel ) {
  echo "<p><a href='$rel->url'>$rel->title</a> ($rel->score)</p>";
 }
} else {
 echo "<p>No related Products found</p>";
}

The module can be found here: https://gist.github.com/3558974

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