mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-17 07:44:24 +00:00
43 lines
1022 B
PHP
43 lines
1022 B
PHP
<?php
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\DataFixtures\Helper;
|
|
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use function random_int;
|
|
|
|
trait RandomPersonHelperTrait
|
|
{
|
|
private ?int $nbOfPersons = null;
|
|
|
|
protected function getRandomPerson(EntityManagerInterface $em): Person
|
|
{
|
|
$qb = $em->createQueryBuilder();
|
|
$qb
|
|
->from(Person::class, 'p');
|
|
|
|
if (null === $this->nbOfPersons) {
|
|
$this->nbOfPersons = $qb
|
|
->select('COUNT(p)')
|
|
->getQuery()
|
|
->getSingleScalarResult();
|
|
}
|
|
|
|
return $qb
|
|
->select('p')
|
|
->setMaxResults(1)
|
|
->setFirstResult(random_int(0, $this->nbOfPersons))
|
|
->getQuery()
|
|
->getSingleResult();
|
|
}
|
|
}
|