|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ORM\Persisters\Entity; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
23
|
|
|
|
|
24
|
|
|
use Doctrine\DBAL\LockMode; |
|
25
|
|
|
use Doctrine\DBAL\Types\Type; |
|
26
|
|
|
|
|
27
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
28
|
|
|
use Doctrine\ORM\Utility\PersisterHelper; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The joined subclass persister maps a single entity instance to several tables in the |
|
32
|
|
|
* database as it is defined by the <tt>Class Table Inheritance</tt> strategy. |
|
33
|
|
|
* |
|
34
|
|
|
* @author Roman Borschel <[email protected]> |
|
35
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
36
|
|
|
* @author Alexander <[email protected]> |
|
37
|
|
|
* @since 2.0 |
|
38
|
|
|
* @see http://martinfowler.com/eaaCatalog/classTableInheritance.html |
|
39
|
|
|
*/ |
|
40
|
|
|
class JoinedSubclassPersister extends AbstractEntityInheritancePersister |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* Map that maps column names to the table names that own them. |
|
44
|
|
|
* This is mainly a temporary cache, used during a single request. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
private $owningTableMap = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Map of table to quoted table names. |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
private $quotedTableMap = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
299 |
|
protected function getDiscriminatorColumnTableName() |
|
61
|
|
|
{ |
|
62
|
299 |
|
$class = ($this->class->name !== $this->class->rootEntityName) |
|
63
|
293 |
|
? $this->em->getClassMetadata($this->class->rootEntityName) |
|
64
|
299 |
|
: $this->class; |
|
65
|
|
|
|
|
66
|
299 |
|
return $class->getTableName(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* This function finds the ClassMetadata instance in an inheritance hierarchy |
|
71
|
|
|
* that is responsible for enabling versioning. |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Doctrine\ORM\Mapping\ClassMetadata |
|
74
|
|
|
*/ |
|
75
|
36 |
|
private function getVersionedClassMetadata() |
|
76
|
|
|
{ |
|
77
|
36 |
|
if (isset($this->class->fieldMappings[$this->class->versionField]['inherited'])) { |
|
78
|
6 |
|
$definingClassName = $this->class->fieldMappings[$this->class->versionField]['inherited']; |
|
79
|
|
|
|
|
80
|
6 |
|
return $this->em->getClassMetadata($definingClassName); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
30 |
|
return $this->class; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets the name of the table that owns the column the given field is mapped to. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $fieldName |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
* |
|
93
|
|
|
* @override |
|
94
|
|
|
*/ |
|
95
|
296 |
|
public function getOwningTable($fieldName) |
|
96
|
|
|
{ |
|
97
|
296 |
|
if (isset($this->owningTableMap[$fieldName])) { |
|
98
|
160 |
|
return $this->owningTableMap[$fieldName]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
switch (true) { |
|
102
|
296 |
|
case isset($this->class->associationMappings[$fieldName]['inherited']): |
|
103
|
260 |
|
$cm = $this->em->getClassMetadata($this->class->associationMappings[$fieldName]['inherited']); |
|
104
|
260 |
|
break; |
|
105
|
|
|
|
|
106
|
245 |
|
case isset($this->class->fieldMappings[$fieldName]['inherited']): |
|
107
|
215 |
|
$cm = $this->em->getClassMetadata($this->class->fieldMappings[$fieldName]['inherited']); |
|
108
|
215 |
|
break; |
|
109
|
|
|
|
|
110
|
|
|
default: |
|
111
|
240 |
|
$cm = $this->class; |
|
112
|
240 |
|
break; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
296 |
|
$tableName = $cm->getTableName(); |
|
116
|
296 |
|
$quotedTableName = $this->quoteStrategy->getTableName($cm, $this->platform); |
|
117
|
|
|
|
|
118
|
296 |
|
$this->owningTableMap[$fieldName] = $tableName; |
|
119
|
296 |
|
$this->quotedTableMap[$tableName] = $quotedTableName; |
|
120
|
|
|
|
|
121
|
296 |
|
return $tableName; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
309 |
|
public function executeInserts() |
|
128
|
|
|
{ |
|
129
|
309 |
|
if ( ! $this->queuedInserts) { |
|
|
|
|
|
|
130
|
218 |
|
return []; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
299 |
|
$postInsertIds = []; |
|
134
|
299 |
|
$idGenerator = $this->class->idGenerator; |
|
135
|
299 |
|
$isPostInsertId = $idGenerator->isPostInsertGenerator(); |
|
136
|
299 |
|
$rootClass = ($this->class->name !== $this->class->rootEntityName) |
|
137
|
293 |
|
? $this->em->getClassMetadata($this->class->rootEntityName) |
|
138
|
299 |
|
: $this->class; |
|
139
|
|
|
|
|
140
|
|
|
// Prepare statement for the root table |
|
141
|
299 |
|
$rootPersister = $this->em->getUnitOfWork()->getEntityPersister($rootClass->name); |
|
142
|
299 |
|
$rootTableName = $rootClass->getTableName(); |
|
143
|
299 |
|
$rootTableStmt = $this->conn->prepare($rootPersister->getInsertSQL()); |
|
144
|
|
|
|
|
145
|
|
|
// Prepare statements for sub tables. |
|
146
|
299 |
|
$subTableStmts = []; |
|
147
|
|
|
|
|
148
|
299 |
|
if ($rootClass !== $this->class) { |
|
149
|
293 |
|
$subTableStmts[$this->class->getTableName()] = $this->conn->prepare($this->getInsertSQL()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
299 |
|
foreach ($this->class->parentClasses as $parentClassName) { |
|
153
|
293 |
|
$parentClass = $this->em->getClassMetadata($parentClassName); |
|
154
|
293 |
|
$parentTableName = $parentClass->getTableName(); |
|
155
|
|
|
|
|
156
|
293 |
|
if ($parentClass !== $rootClass) { |
|
157
|
171 |
|
$parentPersister = $this->em->getUnitOfWork()->getEntityPersister($parentClassName); |
|
158
|
293 |
|
$subTableStmts[$parentTableName] = $this->conn->prepare($parentPersister->getInsertSQL()); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Execute all inserts. For each entity: |
|
163
|
|
|
// 1) Insert on root table |
|
164
|
|
|
// 2) Insert on sub tables |
|
165
|
299 |
|
foreach ($this->queuedInserts as $entity) { |
|
166
|
299 |
|
$insertData = $this->prepareInsertData($entity); |
|
167
|
|
|
|
|
168
|
|
|
// Execute insert on root table |
|
169
|
299 |
|
$paramIndex = 1; |
|
170
|
|
|
|
|
171
|
299 |
|
foreach ($insertData[$rootTableName] as $columnName => $value) { |
|
172
|
299 |
|
$rootTableStmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
299 |
|
$rootTableStmt->execute(); |
|
176
|
|
|
|
|
177
|
299 |
|
if ($isPostInsertId) { |
|
178
|
294 |
|
$generatedId = $idGenerator->generate($this->em, $entity); |
|
179
|
|
|
$id = [ |
|
180
|
294 |
|
$this->class->identifier[0] => $generatedId |
|
181
|
|
|
]; |
|
182
|
294 |
|
$postInsertIds[] = [ |
|
183
|
294 |
|
'generatedId' => $generatedId, |
|
184
|
294 |
|
'entity' => $entity, |
|
185
|
|
|
]; |
|
186
|
|
|
} else { |
|
187
|
5 |
|
$id = $this->em->getUnitOfWork()->getEntityIdentifier($entity); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
299 |
|
if ($this->class->isVersioned) { |
|
191
|
9 |
|
$this->assignDefaultVersionValue($entity, $id); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
// Execute inserts on subtables. |
|
195
|
|
|
// The order doesn't matter because all child tables link to the root table via FK. |
|
196
|
299 |
|
foreach ($subTableStmts as $tableName => $stmt) { |
|
197
|
|
|
/** @var \Doctrine\DBAL\Statement $stmt */ |
|
198
|
293 |
|
$paramIndex = 1; |
|
199
|
293 |
|
$data = $insertData[$tableName] ?? []; |
|
200
|
|
|
|
|
201
|
293 |
|
foreach ((array) $id as $idName => $idVal) { |
|
202
|
293 |
|
$type = isset($this->columnTypes[$idName]) ? $this->columnTypes[$idName] : Type::STRING; |
|
203
|
|
|
|
|
204
|
293 |
|
$stmt->bindValue($paramIndex++, $idVal, $type); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
293 |
|
foreach ($data as $columnName => $value) { |
|
208
|
227 |
|
if (!is_array($id) || !isset($id[$columnName])) { |
|
209
|
227 |
|
$stmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
299 |
|
$stmt->execute(); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
299 |
|
$rootTableStmt->closeCursor(); |
|
218
|
|
|
|
|
219
|
299 |
|
foreach ($subTableStmts as $stmt) { |
|
220
|
293 |
|
$stmt->closeCursor(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
299 |
|
$this->queuedInserts = []; |
|
224
|
|
|
|
|
225
|
299 |
|
return $postInsertIds; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* {@inheritdoc} |
|
230
|
|
|
*/ |
|
231
|
31 |
|
public function update($entity) |
|
232
|
|
|
{ |
|
233
|
31 |
|
$updateData = $this->prepareUpdateData($entity); |
|
234
|
|
|
|
|
235
|
31 |
|
if ( ! $updateData) { |
|
236
|
|
|
return; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
31 |
|
if (($isVersioned = $this->class->isVersioned) === false) { |
|
240
|
|
|
return; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
31 |
|
$versionedClass = $this->getVersionedClassMetadata(); |
|
244
|
31 |
|
$versionedTable = $versionedClass->getTableName(); |
|
245
|
|
|
|
|
246
|
31 |
|
foreach ($updateData as $tableName => $data) { |
|
247
|
31 |
|
$tableName = $this->quotedTableMap[$tableName]; |
|
248
|
31 |
|
$versioned = $isVersioned && $versionedTable === $tableName; |
|
249
|
|
|
|
|
250
|
31 |
|
$this->updateTable($entity, $tableName, $data, $versioned); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
// Make sure the table with the version column is updated even if no columns on that |
|
254
|
|
|
// table were affected. |
|
255
|
30 |
|
if ($isVersioned) { |
|
256
|
5 |
|
if ( ! isset($updateData[$versionedTable])) { |
|
257
|
2 |
|
$tableName = $this->quoteStrategy->getTableName($versionedClass, $this->platform); |
|
258
|
|
|
|
|
259
|
2 |
|
$this->updateTable($entity, $tableName, [], true); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
4 |
|
$identifiers = $this->em->getUnitOfWork()->getEntityIdentifier($entity); |
|
263
|
|
|
|
|
264
|
4 |
|
$this->assignDefaultVersionValue($entity, $identifiers); |
|
265
|
|
|
} |
|
266
|
29 |
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* {@inheritdoc} |
|
270
|
|
|
*/ |
|
271
|
6 |
|
public function delete($entity) |
|
272
|
|
|
{ |
|
273
|
6 |
|
$identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity); |
|
274
|
6 |
|
$id = array_combine($this->class->getIdentifierColumnNames(), $identifier); |
|
275
|
|
|
|
|
276
|
6 |
|
$this->deleteJoinTableRecords($identifier); |
|
277
|
|
|
|
|
278
|
|
|
// If the database platform supports FKs, just |
|
279
|
|
|
// delete the row from the root table. Cascades do the rest. |
|
280
|
6 |
|
if ($this->platform->supportsForeignKeyConstraints()) { |
|
281
|
|
|
$rootClass = $this->em->getClassMetadata($this->class->rootEntityName); |
|
282
|
|
|
$rootTable = $this->quoteStrategy->getTableName($rootClass, $this->platform); |
|
283
|
|
|
$rootTypes = $this->getClassIdentifiersTypes($rootClass); |
|
284
|
|
|
|
|
285
|
|
|
return (bool) $this->conn->delete($rootTable, $id, $rootTypes); |
|
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
// Delete from all tables individually, starting from this class' table up to the root table. |
|
289
|
6 |
|
$rootTable = $this->quoteStrategy->getTableName($this->class, $this->platform); |
|
290
|
6 |
|
$rootTypes = $this->getClassIdentifiersTypes($this->class); |
|
291
|
|
|
|
|
292
|
6 |
|
$affectedRows = $this->conn->delete($rootTable, $id, $rootTypes); |
|
293
|
|
|
|
|
294
|
6 |
|
foreach ($this->class->parentClasses as $parentClass) { |
|
295
|
5 |
|
$parentMetadata = $this->em->getClassMetadata($parentClass); |
|
296
|
5 |
|
$parentTable = $this->quoteStrategy->getTableName($parentMetadata, $this->platform); |
|
297
|
5 |
|
$parentTypes = $this->getClassIdentifiersTypes($parentMetadata); |
|
298
|
|
|
|
|
299
|
5 |
|
$this->conn->delete($parentTable, $id, $parentTypes); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
6 |
|
return (bool) $affectedRows; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* {@inheritdoc} |
|
307
|
|
|
*/ |
|
308
|
71 |
|
public function getSelectSQL($criteria, $assoc = null, $lockMode = null, $limit = null, $offset = null, array $orderBy = null) |
|
309
|
|
|
{ |
|
310
|
71 |
|
$this->switchPersisterContext($offset, $limit); |
|
311
|
|
|
|
|
312
|
71 |
|
$baseTableAlias = $this->getSQLTableAlias($this->class->name); |
|
313
|
71 |
|
$joinSql = $this->getJoinSql($baseTableAlias); |
|
314
|
|
|
|
|
315
|
71 |
|
if ($assoc != null && $assoc['type'] == ClassMetadata::MANY_TO_MANY) { |
|
316
|
2 |
|
$joinSql .= $this->getSelectManyToManyJoinSQL($assoc); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
71 |
|
$conditionSql = ($criteria instanceof Criteria) |
|
320
|
|
|
? $this->getSelectConditionCriteriaSQL($criteria) |
|
321
|
71 |
|
: $this->getSelectConditionSQL($criteria, $assoc); |
|
322
|
|
|
|
|
323
|
|
|
// If the current class in the root entity, add the filters |
|
324
|
71 |
|
if ($filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName))) { |
|
325
|
4 |
|
$conditionSql .= $conditionSql |
|
326
|
2 |
|
? ' AND ' . $filterSql |
|
327
|
4 |
|
: $filterSql; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
71 |
|
$orderBySql = ''; |
|
331
|
|
|
|
|
332
|
71 |
|
if ($assoc !== null && isset($assoc['orderBy'])) { |
|
333
|
1 |
|
$orderBy = $assoc['orderBy']; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
71 |
|
if ($orderBy) { |
|
337
|
1 |
|
$orderBySql = $this->getOrderBySQL($orderBy, $baseTableAlias); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
71 |
|
$lockSql = ''; |
|
341
|
|
|
|
|
342
|
|
|
switch ($lockMode) { |
|
343
|
71 |
|
case LockMode::PESSIMISTIC_READ: |
|
344
|
|
|
|
|
345
|
|
|
$lockSql = ' ' . $this->platform->getReadLockSQL(); |
|
346
|
|
|
|
|
347
|
|
|
break; |
|
348
|
|
|
|
|
349
|
71 |
|
case LockMode::PESSIMISTIC_WRITE: |
|
350
|
|
|
|
|
351
|
|
|
$lockSql = ' ' . $this->platform->getWriteLockSQL(); |
|
352
|
|
|
|
|
353
|
|
|
break; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
71 |
|
$tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); |
|
357
|
71 |
|
$from = ' FROM ' . $tableName . ' ' . $baseTableAlias; |
|
358
|
71 |
|
$where = $conditionSql != '' ? ' WHERE ' . $conditionSql : ''; |
|
359
|
71 |
|
$lock = $this->platform->appendLockHint($from, $lockMode); |
|
360
|
71 |
|
$columnList = $this->getSelectColumnsSQL(); |
|
361
|
71 |
|
$query = 'SELECT ' . $columnList |
|
362
|
71 |
|
. $lock |
|
363
|
71 |
|
. $joinSql |
|
364
|
71 |
|
. $where |
|
365
|
71 |
|
. $orderBySql; |
|
366
|
|
|
|
|
367
|
71 |
|
return $this->platform->modifyLimitQuery($query, $limit, $offset) . $lockSql; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* {@inheritDoc} |
|
372
|
|
|
*/ |
|
373
|
6 |
|
public function getCountSQL($criteria = []) |
|
374
|
|
|
{ |
|
375
|
6 |
|
$tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); |
|
376
|
6 |
|
$baseTableAlias = $this->getSQLTableAlias($this->class->name); |
|
377
|
6 |
|
$joinSql = $this->getJoinSql($baseTableAlias); |
|
378
|
|
|
|
|
379
|
6 |
|
$conditionSql = ($criteria instanceof Criteria) |
|
380
|
6 |
|
? $this->getSelectConditionCriteriaSQL($criteria) |
|
381
|
6 |
|
: $this->getSelectConditionSQL($criteria); |
|
382
|
|
|
|
|
383
|
6 |
|
$filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName)); |
|
384
|
|
|
|
|
385
|
6 |
|
if ('' !== $filterSql) { |
|
386
|
1 |
|
$conditionSql = $conditionSql |
|
387
|
1 |
|
? $conditionSql . ' AND ' . $filterSql |
|
388
|
1 |
|
: $filterSql; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
$sql = 'SELECT COUNT(*) ' |
|
392
|
6 |
|
. 'FROM ' . $tableName . ' ' . $baseTableAlias |
|
393
|
6 |
|
. $joinSql |
|
394
|
6 |
|
. (empty($conditionSql) ? '' : ' WHERE ' . $conditionSql); |
|
395
|
|
|
|
|
396
|
6 |
|
return $sql; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* {@inheritdoc} |
|
401
|
|
|
*/ |
|
402
|
6 |
|
protected function getLockTablesSql($lockMode) |
|
403
|
|
|
{ |
|
404
|
6 |
|
$joinSql = ''; |
|
405
|
6 |
|
$identifierColumns = $this->class->getIdentifierColumnNames(); |
|
406
|
6 |
|
$baseTableAlias = $this->getSQLTableAlias($this->class->name); |
|
407
|
|
|
|
|
408
|
|
|
// INNER JOIN parent tables |
|
409
|
6 |
|
foreach ($this->class->parentClasses as $parentClassName) { |
|
410
|
5 |
|
$conditions = []; |
|
411
|
5 |
|
$tableAlias = $this->getSQLTableAlias($parentClassName); |
|
412
|
5 |
|
$parentClass = $this->em->getClassMetadata($parentClassName); |
|
413
|
5 |
|
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; |
|
414
|
|
|
|
|
415
|
5 |
|
foreach ($identifierColumns as $idColumn) { |
|
416
|
5 |
|
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
5 |
|
$joinSql .= implode(' AND ', $conditions); |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
6 |
|
return parent::getLockTablesSql($lockMode) . $joinSql; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Ensure this method is never called. This persister overrides getSelectEntitiesSQL directly. |
|
427
|
|
|
* |
|
428
|
|
|
* @return string |
|
429
|
|
|
*/ |
|
430
|
71 |
|
protected function getSelectColumnsSQL() |
|
431
|
|
|
{ |
|
432
|
|
|
// Create the column list fragment only once |
|
433
|
71 |
|
if ($this->currentPersisterContext->selectColumnListSql !== null) { |
|
434
|
16 |
|
return $this->currentPersisterContext->selectColumnListSql; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
71 |
|
$columnList = []; |
|
438
|
71 |
|
$discrColumn = $this->class->discriminatorColumn['name']; |
|
439
|
71 |
|
$discrColumnType = $this->class->discriminatorColumn['type']; |
|
440
|
71 |
|
$baseTableAlias = $this->getSQLTableAlias($this->class->name); |
|
441
|
71 |
|
$resultColumnName = $this->platform->getSQLResultCasing($discrColumn); |
|
442
|
|
|
|
|
443
|
71 |
|
$this->currentPersisterContext->rsm->addEntityResult($this->class->name, 'r'); |
|
444
|
71 |
|
$this->currentPersisterContext->rsm->setDiscriminatorColumn('r', $resultColumnName); |
|
445
|
71 |
|
$this->currentPersisterContext->rsm->addMetaResult('r', $resultColumnName, $discrColumn, false, $discrColumnType); |
|
446
|
|
|
|
|
447
|
|
|
// Add regular columns |
|
448
|
71 |
|
foreach ($this->class->fieldMappings as $fieldName => $mapping) { |
|
449
|
71 |
|
$class = isset($mapping['inherited']) |
|
450
|
48 |
|
? $this->em->getClassMetadata($mapping['inherited']) |
|
451
|
71 |
|
: $this->class; |
|
452
|
|
|
|
|
453
|
71 |
|
$columnList[] = $this->getSelectColumnSQL($fieldName, $class); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
// Add foreign key columns |
|
457
|
71 |
|
foreach ($this->class->associationMappings as $mapping) { |
|
458
|
57 |
|
if ( ! $mapping['isOwningSide'] || ! ($mapping['type'] & ClassMetadata::TO_ONE)) { |
|
459
|
44 |
|
continue; |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
56 |
|
$tableAlias = isset($mapping['inherited']) |
|
463
|
39 |
|
? $this->getSQLTableAlias($mapping['inherited']) |
|
464
|
56 |
|
: $baseTableAlias; |
|
465
|
|
|
|
|
466
|
56 |
|
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']); |
|
467
|
|
|
|
|
468
|
56 |
|
foreach ($mapping['joinColumns'] as $joinColumn) { |
|
469
|
56 |
|
$columnList[] = $this->getSelectJoinColumnSQL( |
|
470
|
56 |
|
$tableAlias, |
|
471
|
56 |
|
$joinColumn['name'], |
|
472
|
56 |
|
$this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform), |
|
473
|
56 |
|
PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em) |
|
474
|
|
|
); |
|
475
|
|
|
} |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
// Add discriminator column (DO NOT ALIAS, see AbstractEntityInheritancePersister#processSQLResult). |
|
479
|
71 |
|
$tableAlias = ($this->class->rootEntityName == $this->class->name) |
|
480
|
27 |
|
? $baseTableAlias |
|
481
|
71 |
|
: $this->getSQLTableAlias($this->class->rootEntityName); |
|
482
|
|
|
|
|
483
|
71 |
|
$columnList[] = $tableAlias . '.' . $discrColumn; |
|
484
|
|
|
|
|
485
|
|
|
// sub tables |
|
486
|
71 |
|
foreach ($this->class->subClasses as $subClassName) { |
|
487
|
51 |
|
$subClass = $this->em->getClassMetadata($subClassName); |
|
488
|
51 |
|
$tableAlias = $this->getSQLTableAlias($subClassName); |
|
489
|
|
|
|
|
490
|
|
|
// Add subclass columns |
|
491
|
51 |
|
foreach ($subClass->fieldMappings as $fieldName => $mapping) { |
|
|
|
|
|
|
492
|
51 |
|
if (isset($mapping['inherited'])) { |
|
493
|
51 |
|
continue; |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
46 |
|
$columnList[] = $this->getSelectColumnSQL($fieldName, $subClass); |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
// Add join columns (foreign keys) |
|
500
|
51 |
|
foreach ($subClass->associationMappings as $mapping) { |
|
|
|
|
|
|
501
|
44 |
|
if ( ! $mapping['isOwningSide'] |
|
502
|
44 |
|
|| ! ($mapping['type'] & ClassMetadata::TO_ONE) |
|
503
|
44 |
|
|| isset($mapping['inherited'])) { |
|
504
|
42 |
|
continue; |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
33 |
|
$targetClass = $this->em->getClassMetadata($mapping['targetEntity']); |
|
508
|
|
|
|
|
509
|
33 |
|
foreach ($mapping['joinColumns'] as $joinColumn) { |
|
510
|
33 |
|
$columnList[] = $this->getSelectJoinColumnSQL( |
|
511
|
33 |
|
$tableAlias, |
|
512
|
33 |
|
$joinColumn['name'], |
|
513
|
33 |
|
$this->quoteStrategy->getJoinColumnName($joinColumn, $subClass, $this->platform), |
|
514
|
51 |
|
PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $this->em) |
|
515
|
|
|
); |
|
516
|
|
|
} |
|
517
|
|
|
} |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
71 |
|
$this->currentPersisterContext->selectColumnListSql = implode(', ', $columnList); |
|
521
|
|
|
|
|
522
|
71 |
|
return $this->currentPersisterContext->selectColumnListSql; |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
/** |
|
526
|
|
|
* {@inheritdoc} |
|
527
|
|
|
*/ |
|
528
|
299 |
|
protected function getInsertColumnList() |
|
529
|
|
|
{ |
|
530
|
|
|
// Identifier columns must always come first in the column list of subclasses. |
|
531
|
299 |
|
$columns = $this->class->parentClasses |
|
532
|
293 |
|
? $this->class->getIdentifierColumnNames() |
|
533
|
299 |
|
: []; |
|
534
|
|
|
|
|
535
|
299 |
|
foreach ($this->class->reflFields as $name => $field) { |
|
536
|
299 |
|
if (isset($this->class->fieldMappings[$name]['inherited']) |
|
537
|
293 |
|
&& ! isset($this->class->fieldMappings[$name]['id']) |
|
538
|
299 |
|
|| isset($this->class->associationMappings[$name]['inherited']) |
|
539
|
299 |
|
|| ($this->class->isVersioned && $this->class->versionField == $name) |
|
540
|
299 |
|
|| isset($this->class->embeddedClasses[$name])) { |
|
541
|
278 |
|
continue; |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
299 |
|
if (isset($this->class->associationMappings[$name])) { |
|
545
|
267 |
|
$assoc = $this->class->associationMappings[$name]; |
|
546
|
267 |
|
if ($assoc['type'] & ClassMetadata::TO_ONE && $assoc['isOwningSide']) { |
|
547
|
266 |
|
foreach ($assoc['targetToSourceKeyColumns'] as $sourceCol) { |
|
548
|
267 |
|
$columns[] = $sourceCol; |
|
549
|
|
|
} |
|
550
|
|
|
} |
|
551
|
299 |
|
} else if ($this->class->name != $this->class->rootEntityName || |
|
552
|
299 |
|
! $this->class->isIdGeneratorIdentity() || $this->class->identifier[0] != $name) { |
|
553
|
299 |
|
$columns[] = $this->quoteStrategy->getColumnName($name, $this->class, $this->platform); |
|
554
|
299 |
|
$this->columnTypes[$name] = $this->class->fieldMappings[$name]['type']; |
|
555
|
|
|
} |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
// Add discriminator column if it is the topmost class. |
|
559
|
299 |
|
if ($this->class->name == $this->class->rootEntityName) { |
|
560
|
299 |
|
$columns[] = $this->class->discriminatorColumn['name']; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
299 |
|
return $columns; |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
/** |
|
567
|
|
|
* {@inheritdoc} |
|
568
|
|
|
*/ |
|
569
|
9 |
|
protected function assignDefaultVersionValue($entity, array $id) |
|
570
|
|
|
{ |
|
571
|
9 |
|
$value = $this->fetchVersionValue($this->getVersionedClassMetadata(), $id); |
|
572
|
9 |
|
$this->class->setFieldValue($entity, $this->class->versionField, $value); |
|
573
|
9 |
|
} |
|
574
|
|
|
|
|
575
|
|
|
/** |
|
576
|
|
|
* @param string $baseTableAlias |
|
577
|
|
|
* |
|
578
|
|
|
* @return string |
|
579
|
|
|
*/ |
|
580
|
76 |
|
private function getJoinSql($baseTableAlias) |
|
581
|
|
|
{ |
|
582
|
76 |
|
$joinSql = ''; |
|
583
|
76 |
|
$identifierColumn = $this->class->getIdentifierColumnNames(); |
|
584
|
|
|
|
|
585
|
|
|
// INNER JOIN parent tables |
|
586
|
76 |
|
foreach ($this->class->parentClasses as $parentClassName) { |
|
587
|
52 |
|
$conditions = []; |
|
588
|
52 |
|
$parentClass = $this->em->getClassMetadata($parentClassName); |
|
589
|
52 |
|
$tableAlias = $this->getSQLTableAlias($parentClassName); |
|
590
|
52 |
|
$joinSql .= ' INNER JOIN ' . $this->quoteStrategy->getTableName($parentClass, $this->platform) . ' ' . $tableAlias . ' ON '; |
|
591
|
|
|
|
|
592
|
|
|
|
|
593
|
52 |
|
foreach ($identifierColumn as $idColumn) { |
|
594
|
52 |
|
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; |
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
52 |
|
$joinSql .= implode(' AND ', $conditions); |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
// OUTER JOIN sub tables |
|
601
|
76 |
|
foreach ($this->class->subClasses as $subClassName) { |
|
602
|
53 |
|
$conditions = []; |
|
603
|
53 |
|
$subClass = $this->em->getClassMetadata($subClassName); |
|
604
|
53 |
|
$tableAlias = $this->getSQLTableAlias($subClassName); |
|
605
|
53 |
|
$joinSql .= ' LEFT JOIN ' . $this->quoteStrategy->getTableName($subClass, $this->platform) . ' ' . $tableAlias . ' ON '; |
|
606
|
|
|
|
|
607
|
53 |
|
foreach ($identifierColumn as $idColumn) { |
|
608
|
53 |
|
$conditions[] = $baseTableAlias . '.' . $idColumn . ' = ' . $tableAlias . '.' . $idColumn; |
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
53 |
|
$joinSql .= implode(' AND ', $conditions); |
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
76 |
|
return $joinSql; |
|
615
|
|
|
} |
|
616
|
|
|
} |
|
617
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.