countPersons) { $qb = $em->createQueryBuilder(); $this->countPersons = $qb->select('count(p)') ->from(Person::class, 'p') ->getQuery() ->getSingleScalarResult(); } if ([] === $this->randPersons) { $qb = $em->createQueryBuilder(); $this->randPersons = $qb ->select('p') ->from(Person::class, 'p') ->getQuery() ->setFirstResult(\random_int(0, $this->countPersons - $fetchBy)) ->setMaxResults($fetchBy) ->getResult(); } return \array_pop($this->randPersons); } }