XF 2.2 Ordering finder results

Orit

Active member
Hello
I have a simple finder that fetches the last 5 posts posted on a specific thread.

Sometimes I'd like to order the posts in an ASC order, other times I'd like to order them DESC.
How can I achieve that?

I now have 2 finders:
PHP:
$postFinder = \XF::finder('XF:Post')
    ->where('thread_id', $threadId)
    ->order('post_id', 'DESC')
    ->limit($limit);

$reversedPostFinder = \XF::finder('XF:Post')
    ->where('thread_id', $threadId)
    ->order('post_id', 'ASC')
    ->limit($limit)
    ->order('post_id', 'DESC');

But I was wondering if there is a way to simply re-order the finder results.

Thanks!!
 
is there a condition when you want to use one over the other?
Just ordering the ids in asc instead of desc and vice versa.
It's an option I want to let users choose when setting up a specific widget.
 
Just ordering the ids in asc instead of desc and vice versa.
It's an option I want to let users choose when setting up a specific widget.
I thought you wanted to re-order results after fetching. If you want to change the query order, just set the direction conditionally:

PHP:
$direction = $someOption ? 'asc' : 'desc';
$postFinder = \XF::finder('XF:Post')
    ->where('thread_id', $threadId)
    ->order('post_id', $direction)
    ->limit($limit);
 
I thought you wanted to re-order results after fetching. If you want to change the query order, just set the direction conditionally:

PHP:
$direction = $someOption ? 'asc' : 'desc';
$postFinder = \XF::finder('XF:Post')
    ->where('thread_id', $threadId)
    ->order('post_id', $direction)
    ->limit($limit);
thank you!
Won't that query return the first X ids or the last X ids?

I want to return the last x posts, but order their ids either asc or desc.
When thinking about it a bit more, I have an if condition so only one of the finders gets called each time. I don't think it really matters....
 
Oh, then you want to reverse the collection and not the finder (query). You can just use $results->reverse() conditionally.
 
Oh, then you want to reverse the collection and not the finder (query). You can just use $results->reverse() conditionally.
Thank you so much!!!
I knew I was thinking too hard :ROFLMAO:

BTW, the code I posted above does not work. I converted the collection to an array and reversed it.
I presume the reverse() function does just that...
 
You'd have to fetch the result collection first to use the reverse method, and the method returns a new collection:

PHP:
$postFinder = \XF::finder('XF:Post')
    ->where('thread_id', $threadId)
    ->order('post_id', 'DESC')
    ->limit($limit);
$posts = $postFinder->fetch();

$reverse = true; // replace with your option or whatever
if ($reverse)
{
    $posts = $posts->reverse();
}
 
You'd have to fetch the result collection first to use the reverse method, and the method returns a new collection:

PHP:
$postFinder = \XF::finder('XF:Post')
    ->where('thread_id', $threadId)
    ->order('post_id', 'DESC')
    ->limit($limit);
$posts = $postFinder->fetch();

$reverse = true; // replace with your option or whatever
if ($reverse)
{
    $posts = $posts->reverse();
}
Thank you so much for all your help!
 
Back
Top Bottom