src/Repository/ItemRepository.php
public function findRandom(int $numberOfItems, array $modCombinationIds = []): array
    {
        $queryBuilder = $this->entityManager->createQueryBuilder();
        $queryBuilder->select(['i', 'RAND() AS HIDDEN rand'])
                     ->from(Item::class, 'i')
                     ->addOrderBy('rand')
                     ->setMaxResults($numberOfItems);

        if (count($modCombinationIds) > 0) {
            $queryBuilder->innerJoin('i.modCombinations', 'mc')
                         ->andWhere('mc.id IN (:modCombinationIds)')
                         ->setParameter('modCombinationIds', array_values($modCombinationIds));
        }
        return $queryBuilder->getQuery()->getResult();
    }
public function findRandom(int $numberOfItems, array $modCombinationIds = []): array
    {
        $queryBuilder = $this->entityManager->createQueryBuilder();
        $queryBuilder->select(['i', 'RAND() AS HIDDEN rand'])
                     ->from(Item::class, 'i')
                     ->addOrderBy('rand')
                     ->setMaxResults($numberOfItems);

        if (count($modCombinationIds) > 0) {
            $queryBuilder->innerJoin('i.modCombinations', 'mc')
                         ->andWhere('mc.id IN (:modCombinationIds)')
                         ->setParameter('modCombinationIds', array_values($modCombinationIds));
        }
        return $queryBuilder->getQuery()->getResult();
    }
public function findRandom ( int $ numberOfItems , array $ modCombinationIds = [ ] ) : array { $ queryBuilder = $ this -> entityManager -> createQueryBuilder ( ) ; $ queryBuilder -> select ( [ 'i' , 'RAND() AS HIDDEN rand' ] ) -> from ( Item :: class , 'i' ) -> addOrderBy ( 'rand' ) -> setMaxResults ( $ numberOfItems ) ; if ( count ( $ modCombinationIds ) > 0 ) { $ queryBuilder -> innerJoin ( 'i.modCombinations' , 'mc' ) -> andWhere ( 'mc.id IN (:modCombinationIds)' ) -> setParameter ( 'modCombinationIds' , array_values ( $ modCombinationIds ) ) ; } return $ queryBuilder -> getQuery ( ) -> getResult ( ) ; }
Finds random items.
@param int $numberOfItems
@param array|int[] $modCombinationIds
@return array|Item[]
Finds random items .