Fixed Error when creating member statistic using relational field

Jon W

Well-known member
Affected version
2.1.1
I added Profile.location to userSearcherOrders and it works fine unless I set it to show the value, in which case I get the following.

TypeError: Argument 2 passed to XF\Service\MemberStat\Preparer::prepareCacheResults() must be of the type array, object given, called in /home/defence/public_html/src/XF/Service/MemberStat/Preparer.php on line 236 in src/XF/Service/MemberStat/Preparer.php at line 156
  1. XF\Service\MemberStat\Preparer->prepareCacheResults() in src/XF/Service/MemberStat/Preparer.php at line 236
  2. XF\Service\MemberStat\Preparer->getResultsData() in src/XF/Service/MemberStat/Preparer.php at line 100
  3. XF\Service\MemberStat\Preparer->getResults() in src/XF/Entity/MemberStat.php at line 105
  4. XF\Entity\MemberStat->getResults() in src/XF/Pub/Controller/Member.php at line 114
  5. XF\Pub\Controller\Member->actionIndex() in src/XF/Mvc/Dispatcher.php at line 321
  6. XF\Mvc\Dispatcher->dispatchClass() in src/XF/Mvc/Dispatcher.php at line 248
  7. XF\Mvc\Dispatcher->dispatchFromMatch() in src/XF/Mvc/Dispatcher.php at line 100
  8. XF\Mvc\Dispatcher->dispatchLoop() in src/XF/Mvc/Dispatcher.php at line 50
  9. XF\Mvc\Dispatcher->run() in src/XF/App.php at line 2177
  10. XF\App->run() in src/XF.php at line 390
  11. XF::runApp() in index.php at line 20
 
Thanks for reporting this.

The main bug here is actually here:
PHP:
$results = $results->pluckNamed($valueField, 'user_id');
If pluckNamed returns an empty result set, we automatically infer that it should return an ArrayCollection object, rather than an array, but the call to prepareCacheResults expects an array, therefore you get the error. So we've fixed that.

Though you may be wondering why your result set would be empty. This is because the field you are ordering on is a string and it isn't marked as such (see stringSortOrders inside the XF\Searcher\User class).

If it's a numeric field and a value is shown (as most should be, because they are mostly for statistics) then we apply some conditions to filter out 0 values which wouldn't work for a string field.

Then, by default, we wouldn't show a value for a string based field anyway, so you'd likely need to use a callback to display that.

For now, modifying stringSortOrders means you would need a class extension, but part of the changes I've made is passing that value to the user_searcher_orders code event so it can be done at the same time as adding your searcher order.
 
Thank you for reporting this issue. It has now been resolved and we are aiming to include it in a future XF release (2.1.2).

Change log:
Prevent an error in the member stats system when an empty result set is returned as an object instead of the expected array.
Any changes made as a result of this issue being resolved may not be rolled out here until later.
 
Top Bottom