1

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

3 Answers 3

1

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 Commented May 21, 2020 at 7:54
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
  • 1
    Good suggestion however I meant MySQL random sort eg ORDER BY RAND() Commented May 18, 2020 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
    Commented May 18, 2020 at 14:20
  • Have you got an example on what a RepositoryCollection might look like? Commented May 18, 2020 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
    Commented May 19, 2020 at 7:28
  • That would randomly sort in either direction. Whereas Mysql RAND() is a random sort Commented May 19, 2020 at 7:40
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 and acknowledge you have read our privacy policy.

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