-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathDoctrineWriter.php
More file actions
304 lines (266 loc) · 7.54 KB
/
Copy pathDoctrineWriter.php
File metadata and controls
304 lines (266 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
namespace Ddeboer\DataImport\Writer;
use Ddeboer\DataImport\Writer;
use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
/**
* A bulk Doctrine writer
*
* See also the {@link http://www.doctrine-project.org/docs/orm/2.1/en/reference/batch-processing.html Doctrine documentation}
* on batch processing.
*
* @author David de Boer <david@ddeboer.nl>
*/
class DoctrineWriter implements Writer, FlushableWriter
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var string
*/
protected $entityName;
/**
* @var EntityRepository
*/
protected $entityRepository;
/**
* @var ClassMetadata
*/
protected $entityMetadata;
/**
* Original Doctrine logger
*
* @var SQLLogger
*/
protected $originalLogger;
/**
* Whether to truncate the table first
*
* @var boolean
*/
protected $truncate = true;
/**
* List of fields used to lookup an entity
*
* @var array
*/
protected $lookupFields = array();
/**
* @param EntityManagerInterface $entityManager
* @param string $entityName
* @param string|array $index Field or fields to find current entities by
*/
public function __construct(EntityManagerInterface $entityManager, $entityName, $index = null)
{
$this->entityManager = $entityManager;
$this->entityRepository = $entityManager->getRepository($entityName);
$this->entityMetadata = $entityManager->getClassMetadata($entityName);
//translate entityName in case a namespace alias is used
$this->entityName = $this->entityMetadata->getName();
if ($index) {
if (is_array($index)) {
$this->lookupFields = $index;
} else {
$this->lookupFields = [$index];
}
}
}
/**
* @return boolean
*/
public function getTruncate()
{
return $this->truncate;
}
/**
* Set whether to truncate the table first
*
* @param boolean $truncate
*
* @return $this
*/
public function setTruncate($truncate)
{
$this->truncate = $truncate;
return $this;
}
/**
* Disable truncation
*
* @return $this
*/
public function disableTruncate()
{
$this->truncate = false;
return $this;
}
/**
* Disable Doctrine logging
*
* @return $this
*/
public function prepare()
{
$this->disableLogging();
if (true === $this->truncate) {
$this->truncateTable();
}
}
/**
* Return a new instance of the entity
*
* @return object
*/
protected function getNewInstance()
{
if (class_exists($this->entityName) === false) {
throw new \Exception('Unable to create new instance of ' . $this->entityName);
}
return new $this->entityName;
}
/**
* Call a setter of the entity
*
* @param object $entity
* @param mixed $value
* @param string $setter
*/
protected function setValue($entity, $value, $setter)
{
if (method_exists($entity, $setter)) {
$entity->$setter($value);
}
}
/**
* Re-enable Doctrine logging
*
* @return $this
*/
public function finish()
{
$this->flush();
$this->reEnableLogging();
}
/**
* {@inheritdoc}
*/
public function writeItem(array $item)
{
$entity = $this->findOrCreateItem($item);
$this->loadAssociationObjectsToEntity($item, $entity);
$this->updateEntity($item, $entity);
$this->entityManager->persist($entity);
}
/**
* @param array $item
* @param object $entity
*/
protected function updateEntity(array $item, $entity)
{
$fieldNames = array_merge($this->entityMetadata->getFieldNames(), $this->entityMetadata->getAssociationNames());
foreach ($fieldNames as $fieldName) {
$value = null;
if (isset($item[$fieldName])) {
$value = $item[$fieldName];
} elseif (method_exists($item, 'get' . ucfirst($fieldName))) {
$value = $item->{'get' . ucfirst($fieldName)};
}
if (null === $value) {
continue;
}
if (!($value instanceof \DateTime)
|| $value != $this->entityMetadata->getFieldValue($entity, $fieldName)
) {
$setter = 'set' . ucfirst($fieldName);
$this->setValue($entity, $value, $setter);
}
}
}
/**
* Add the associated objects in case the item have for persist its relation
*
* @param array $item
* @param object $entity
*/
protected function loadAssociationObjectsToEntity(array $item, $entity)
{
foreach ($this->entityMetadata->getAssociationMappings() as $associationMapping) {
$value = null;
if (isset($item[$associationMapping['fieldName']]) && !is_object($item[$associationMapping['fieldName']])) {
$value = $this->entityManager->getReference($associationMapping['targetEntity'], $item[$associationMapping['fieldName']]);
}
if (null === $value) {
continue;
}
$setter = 'set' . ucfirst($associationMapping['fieldName']);
$this->setValue($entity, $value, $setter);
}
}
/**
* Truncate the database table for this writer
*/
protected function truncateTable()
{
$tableName = $this->entityMetadata->table['name'];
$connection = $this->entityManager->getConnection();
$query = $connection->getDatabasePlatform()->getTruncateTableSQL($tableName, true);
$connection->executeQuery($query);
}
/**
* Disable Doctrine logging
*/
protected function disableLogging()
{
$config = $this->entityManager->getConnection()->getConfiguration();
$this->originalLogger = $config->getSQLLogger();
$config->setSQLLogger(null);
}
/**
* Re-enable Doctrine logging
*/
protected function reEnableLogging()
{
$config = $this->entityManager->getConnection()->getConfiguration();
$config->setSQLLogger($this->originalLogger);
}
/**
* Finds existing entity or create a new instance
*
* @param array $item
*/
protected function findOrCreateItem(array $item)
{
$entity = null;
// If the table was not truncated to begin with, find current entity
// first
if (false === $this->truncate) {
if (!empty($this->lookupFields)) {
$lookupConditions = array();
foreach ($this->lookupFields as $fieldName) {
$lookupConditions[$fieldName] = $item[$fieldName];
}
$entity = $this->entityRepository->findOneBy(
$lookupConditions
);
} else {
$entity = $this->entityRepository->find(current($item));
}
}
if (!$entity) {
return $this->getNewInstance();
}
return $entity;
}
/**
* Flush and clear the entity manager
*/
public function flush()
{
$this->entityManager->flush();
$this->entityManager->clear($this->entityName);
}
}