|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Doctrine\ORM\Populator; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Doctrine\ORM\EntityRepository; |
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
9
|
|
|
use Nimble\ElasticBundle\Populator\PopulationFetcherInterface; |
|
10
|
|
|
|
|
11
|
|
|
class DoctrineORMPopulationFetcher implements PopulationFetcherInterface |
|
12
|
|
|
{ |
|
13
|
|
|
const ROOT_ALIAS = 'e'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var EntityRepository |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $entityRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ClassMetadata |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $entityClassMetadata; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var EntityManager |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $entityManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param EntityManager $entityManager |
|
32
|
|
|
* @param string $entityName |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(EntityManager $entityManager, $entityName) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$this->entityManager = $entityManager; |
|
37
|
|
|
$this->entityRepository = $entityManager->getRepository($entityName); |
|
38
|
|
|
$this->entityClassMetadata = $entityManager->getClassMetadata( |
|
39
|
|
|
$this->entityRepository->getClassName() |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return QueryBuilder |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function createQueryBuilder() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->entityRepository->createQueryBuilder(self::ROOT_ALIAS); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getEntityCount() |
|
55
|
|
|
{ |
|
56
|
|
|
$qb = $this->createQueryBuilder(); |
|
57
|
|
|
|
|
58
|
|
|
$qb->select($qb->expr()->count(self::ROOT_ALIAS)); |
|
59
|
|
|
|
|
60
|
|
|
return $qb->getQuery()->getSingleScalarResult(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function fetchEntities($offset = 0, $limit = null) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->entityManager->clear(); /* TODO: Check if this gives us any value. */ |
|
69
|
|
|
|
|
70
|
|
|
$qb = $this->createQueryBuilder(); |
|
71
|
|
|
|
|
72
|
|
|
$qb->select(self::ROOT_ALIAS); |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->entityClassMetadata->getIdentifierFieldNames() as $fieldName) { |
|
75
|
|
|
$qb->addOrderBy(sprintf('%s.%s', self::ROOT_ALIAS, $fieldName)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$qb->setFirstResult($offset); |
|
79
|
|
|
|
|
80
|
|
|
if (null !== $limit) { |
|
81
|
|
|
$qb->setMaxResults($limit); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $qb->getQuery()->getResult(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.