mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-04-02 19:13:42 +00:00
- Implemented `getRandomUser` method to fetch a random user using `EntityManagerInterface`. - Added support for random user retrieval by querying `User` entity repository.
31 lines
746 B
PHP
31 lines
746 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Test;
|
|
|
|
use Chill\MainBundle\Entity\User;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
trait RandomUserTrait
|
|
{
|
|
public function getRandomUser(EntityManagerInterface $em): User
|
|
{
|
|
$userRepository = $em->getRepository(User::class);
|
|
$count = $userRepository->count([]);
|
|
$random = mt_rand(0, $count - 1);
|
|
|
|
return $em->createQuery('SELECT u FROM '.User::class.' u ')
|
|
->setMaxResults(1)
|
|
->setFirstResult($random)
|
|
->getSingleResult();
|
|
}
|
|
}
|