XF 2.2 order by a desc, b desc

Robert9

Well-known member
$itemFinder->order('rating_avg', desc, 'rating_count', 'desc')

It seems this does not work.
How can use two columns, please?
 
InvalidArgumentException: Unknown column desc on R9\Item:Item in src/XF/Mvc/Entity/Finder.php at line 1694

$itemFinder->order(['comment_count', 'create_date' ], 'desc');

=>

ORDER BY xf_r9_item_item.comment_count DESC, xf_r9_item_item.create_date DESC


but i need different directions also

ORDER BY xf_r9_item_item.comment_count ASC, xf_r9_item_item.create_date DESC
 
Last edited:
Yeah, so that’s not the same format as I posted. It needs to be a key => val array like my example, you can specify a different order for each field.
 
Thank you, but your code does not work for me:

$itemFinder->order(['comment_count' => 'asc', 'create_date' => 'desc']);

=> error-message ist on this line and says that there is no column: asc


This code works, but miss different directions

$itemFinder->order(['comment_count', 'create_date'],'desc');
 
Oh, my bad:

$itemFinder->order([['comment_count', 'asc'],['create_date', 'desc']])

Tested it this time ;)
 
Last edited:
Very nice, now i can do:

$itemFinder->order('rating_count', $direction)->order('rating_avg','desc')->order('create_date','desc');

real example:

ORDER BY `xf_r9_item_item`.`rating_count` DESC, `xf_r9_item_item`.`rating_avg` DESC, `xf_r9_item_item`.`create_date` DESC;
 
I guess, i have tried this, but will try again. Thank you.

To add ->order->order ... makes sense for me, because i can save some code.
 
Is there any difference?

$itemFinder->order('comment_count', $direction)->order('create_date','desc');

$itemFinder->order([['comment_count', $direction],['create_date', 'desc']]);
 
Just personal preference, usually I'd break the array out onto multiple lines:

Code:
$itemFinder->order([
    ['comment_count', $direction],
    ['create_date', 'desc'],
]);

You could put each order call on a new line too to help readability. Personally I like to only call each function once where possible.
 
Back
Top Bottom