0

Is it possible to have a random sort when searching with repositories? Checked dev docs and unsure. Not found much elsewhere.

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/searching-with-repositories.html#sorting

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SortOrderFactory;
$this->searchCriteriaBuilder->addFilter(MessageInterface::STATUS, [Data::ENABLED], 'eq');
$randomSort = $this->sortOrderFactory
    ->create()
    ->setField(MessageInterface::SORT)
    ->setDirection(SortOrder::SORT_ASC); // random sort here
$this->searchCriteriaBuilder->setSortOrders([$randomSort]);
$searchCriteria = $this->searchCriteriaBuilder->create();
return $this->messageRepositoryInterface->getList($searchCriteria)->getItems();
1
0

You can use the built-in method array_rand to get either SORT_ASC or SORT_DESC. Something like this:

$this->searchCriteriaBuilder->addFilter(MessageInterface::STATUS, [Data::ENABLED], 'eq');

$directions = [SortOrder::SORT_ASC, SortOrder::SORT_DESC];
$randomDirection = $directions[array_rand($directions)];

$randomSort = $this->sortOrderFactory
    ->create()
    ->setField(MessageInterface::SORT)
    ->setDirection(randomDirection); // random sort here
$this->searchCriteriaBuilder->setSortOrders([$randomSort]);
$searchCriteria = $this->searchCriteriaBuilder->create();
return $this->messageRepositoryInterface->getList($searchCriteria)->getItems();
5
  • Good suggestion however I meant MySQL random sort eg ORDER BY RAND() – Dominic Xigen May 18 '20 at 11:51
  • You can't use ORDER BY RAND() like that as SortOrder class validate the Direction to be either ASC or DESC. See the validation in \Magento\Framework\Api\SortOrder::validateDirectionIsAscOrDesc I think you might want to switch to use MessageRepositoryCollection so you can use ORDER BY RAND() – Toan Tam May 18 '20 at 14:20
  • Have you got an example on what a RepositoryCollection might look like? – Dominic Xigen May 18 '20 at 15:31
  • Sorry, it is just my guess. I haven't used it before so I can't tell. Why can't you use array_rand() in PHP for it? – Toan Tam May 19 '20 at 7:28
  • That would randomly sort in either direction. Whereas Mysql RAND() is a random sort – Dominic Xigen May 19 '20 at 7:40
0

Got a solution.

$result = $this->messageRepositoryInterface->getList($searchCriteria)->getItems();
if ($group->getSortby() == Data::RANDOM) {
    shuffle($result);
    return $result;
}
return $result;
1
  • Only issue remains is that when a limit is introduced i.e. fetch only 1 record - it always randomly selects the same record - so not true RAND() but good enough for now – Dominic Xigen May 21 '20 at 7:54
0

Missing feature but very difficult to implement

https://github.com/magento/magento2/issues/28315

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.