Merge commit '98760bc5'

This commit is contained in:
2021-11-22 11:10:51 +01:00
6 changed files with 779 additions and 140 deletions

View File

@@ -0,0 +1,35 @@
<?php
namespace Chill\PersonBundle\DataFixtures\Helper;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface;
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()
;
}
}