Jump to content

Find Child's Parent's Children


dps123
 Share

Recommended Posts

So, here I am, working at the weekend again to get this site done. I'm hoping someone can help me get over this hurdle. I'll try to explain it as best as I can.

I have notes pages using a template 'note'.

Children of those pages are comments, using a template 'comment'.

When a comment is posted, their username is recorded in the field comment_username attached to the comment template.

There is one particularly important commenter. Let's call him John. What I need to do is select new comments where at least one of the comments before that was from John.

I'm trying all kinds of crazy $pages->find things, but going around in circles!

Link to comment
Share on other sites

I'm don't follow exactly what you're wanting to do. For instance, child's parent's children is the same as just children and "new" and "before" is ambiguous. But maybe this works...

$johns_comments = $pages->find("template=comment, comment_username=John");
$comments_after_johns = new PageArray;
foreach($johns_comments as $j_comment) {
    $comments_after_johns->add($j_comment->nextAll);
}

Not tested.

Link to comment
Share on other sites


$johns = $pages->find("template=comment, comment_username=John");

$parents = $johns->explode(function($c){ return $c->parent->id; });

$parents = array_unique($parents);

$repliesToJohn = new PageArray();

foreach($parents as $parent){

$johnsFirstComment = $johns->find("parent=$parent, sort=created");

$afterJohnsFirstComment = $pages->find("template=comment, parent=$parent, created>$johnsFirstComment->created, comment_username!=John");

$repliesToJohn->import($afterJohnsFirstComment);

}

  • Like 1
Link to comment
Share on other sites

You can make it work with pagination, but it's a bit different to execute.

// This part cannot use pagination, because we need all of john's comments,
// but the whole selector construction is cacheable to minimize load
// invalidate the cache whenever john does post a comment
// optionally you can then also recreate the selector, so the waiting time 
// does hit John and not other users
$johns = $pages->find("template=comment, comment_username=John");
$parents = $johns->explode(function($c){ return $c->parent->id; });
$parents = array_unique($parents);

$commentsSelector = array();
foreach($parents as $parent){
  $johnsFirstComment = $johns->find("parent=$parent, sort=created");
  $commentsSelector[] = "afterjohn=(template=comment, parent=$parent, created>$johnsFirstComment->created, comment_username!=John)";
}

$commentsSelector = implode(', ', $commentsSelector);
// End cacheable

$pages->find($commentsSelector . ', sort=created, limit=10');
  • 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...