1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repository; |
4
|
|
|
|
5
|
|
|
use App\Entity\Employee; |
6
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
7
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
8
|
|
|
|
9
|
|
|
class EmployeeRepository extends AbstractRepository |
10
|
|
|
{ |
11
|
|
|
private TranslatorInterface $translator; |
|
|
|
|
12
|
|
|
|
13
|
|
|
public function __construct(ManagerRegistry $registry, TranslatorInterface $translator) |
14
|
|
|
{ |
15
|
|
|
parent::__construct($registry, Employee::class); |
16
|
|
|
$this->translator = $translator; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return Employee[] |
21
|
|
|
* @throws \Doctrine\ORM\ORMException |
22
|
|
|
*/ |
23
|
|
|
public function rand(int $limit, int $page, int $seed, string $locale): array |
24
|
|
|
{ |
25
|
|
|
$qb = $this->createQueryBuilder('e') |
26
|
|
|
->setFirstResult(($page-1) * $limit) |
27
|
|
|
->setMaxResults($limit); |
28
|
|
|
if (0 != $seed) { |
29
|
|
|
$qb->orderBy('RAND(:seed)') |
30
|
|
|
->setParameter('seed', $seed); |
31
|
|
|
} else { |
32
|
|
|
$qb->orderBy('e.lastName', 'ASC'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$employees = $qb |
36
|
|
|
->getQuery() |
37
|
|
|
->execute() |
38
|
|
|
; |
39
|
|
|
|
40
|
|
|
$employeesTranslated = []; |
41
|
|
|
|
42
|
|
|
foreach ($employees as $employee) { |
43
|
|
|
$employee->setLocale($locale); |
44
|
|
|
$this->_em->refresh($employee); |
45
|
|
|
|
46
|
|
|
if ($employee->getTranslations()) { |
47
|
|
|
$employee->unsetTranslations(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->translator->setLocale($locale); |
51
|
|
|
$employee->setPosition($this->translator->trans($employee->getPosition())); |
52
|
|
|
|
53
|
|
|
$employeesTranslated[] = $employee; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $employeesTranslated; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|