1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Mouf\Database\TDBM\Utils; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Schema\Column; |
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
9
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
10
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
11
|
|
|
use Mouf\Database\TDBM\TDBMException; |
12
|
|
|
use Mouf\Database\TDBM\TDBMSchemaAnalyzer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class represents a bean |
16
|
|
|
*/ |
17
|
|
|
class BeanDescriptor |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Table |
21
|
|
|
*/ |
22
|
|
|
private $table; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SchemaAnalyzer |
26
|
|
|
*/ |
27
|
|
|
private $schemaAnalyzer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Schema |
31
|
|
|
*/ |
32
|
|
|
private $schema; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AbstractBeanPropertyDescriptor[] |
36
|
|
|
*/ |
37
|
|
|
private $beanPropertyDescriptors = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TDBMSchemaAnalyzer |
41
|
|
|
*/ |
42
|
|
|
private $tdbmSchemaAnalyzer; |
43
|
|
|
|
44
|
|
View Code Duplication |
public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) { |
|
|
|
|
45
|
|
|
$this->table = $table; |
46
|
|
|
$this->schemaAnalyzer = $schemaAnalyzer; |
47
|
|
|
$this->schema = $schema; |
48
|
|
|
$this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
49
|
|
|
$this->initBeanPropertyDescriptors(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function initBeanPropertyDescriptors() { |
53
|
|
|
$this->beanPropertyDescriptors = $this->getProperties($this->table); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the foreignkey the column is part of, if any. null otherwise. |
58
|
|
|
* |
59
|
|
|
* @param Table $table |
60
|
|
|
* @param Column $column |
61
|
|
|
* @return ForeignKeyConstraint|null |
62
|
|
|
*/ |
63
|
|
|
private function isPartOfForeignKey(Table $table, Column $column) { |
64
|
|
|
$localColumnName = $column->getName(); |
65
|
|
|
foreach ($table->getForeignKeys() as $foreignKey) { |
66
|
|
|
foreach ($foreignKey->getColumns() as $columnName) { |
67
|
|
|
if ($columnName === $localColumnName) { |
|
|
|
|
68
|
|
|
return $foreignKey; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return AbstractBeanPropertyDescriptor[] |
77
|
|
|
*/ |
78
|
|
|
public function getBeanPropertyDescriptors() |
79
|
|
|
{ |
80
|
|
|
return $this->beanPropertyDescriptors; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
85
|
|
|
* |
86
|
|
|
* @return AbstractBeanPropertyDescriptor[] |
87
|
|
|
*/ |
88
|
|
|
public function getConstructorProperties() { |
89
|
|
|
|
90
|
|
|
$constructorProperties = array_filter($this->beanPropertyDescriptors, function(AbstractBeanPropertyDescriptor $property) { |
91
|
|
|
return $property->isCompulsory(); |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
return $constructorProperties; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the list of properties exposed as getters and setters in this class. |
99
|
|
|
* |
100
|
|
|
* @return AbstractBeanPropertyDescriptor[] |
101
|
|
|
*/ |
102
|
|
|
public function getExposedProperties() { |
103
|
|
|
$exposedProperties = array_filter($this->beanPropertyDescriptors, function(AbstractBeanPropertyDescriptor $property) { |
104
|
|
|
return $property->getTable()->getName() == $this->table->getName(); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
return $exposedProperties; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the list of properties for this table (including parent tables). |
112
|
|
|
* |
113
|
|
|
* @param Table $table |
114
|
|
|
* @return AbstractBeanPropertyDescriptor[] |
115
|
|
|
*/ |
116
|
|
|
private function getProperties(Table $table) |
117
|
|
|
{ |
118
|
|
|
$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
119
|
|
|
if ($parentRelationship) { |
120
|
|
|
$parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
121
|
|
|
$properties = $this->getProperties($parentTable); |
122
|
|
|
// we merge properties by overriding property names. |
123
|
|
|
$localProperties = $this->getPropertiesForTable($table); |
124
|
|
|
foreach ($localProperties as $name => $property) { |
125
|
|
|
// We do not override properties if this is a primary key! |
126
|
|
|
if ($property->isPrimaryKey()) { |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
$properties[$name] = $property; |
130
|
|
|
} |
131
|
|
|
//$properties = array_merge($properties, $localProperties); |
|
|
|
|
132
|
|
|
|
133
|
|
|
} else { |
134
|
|
|
$properties = $this->getPropertiesForTable($table); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $properties; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns the list of properties for this table (ignoring parent tables). |
142
|
|
|
* |
143
|
|
|
* @param Table $table |
144
|
|
|
* @return AbstractBeanPropertyDescriptor[] |
145
|
|
|
*/ |
146
|
|
|
private function getPropertiesForTable(Table $table) |
147
|
|
|
{ |
148
|
|
|
$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
149
|
|
|
if ($parentRelationship) { |
150
|
|
|
$ignoreColumns = $parentRelationship->getLocalColumns(); |
151
|
|
|
} else { |
152
|
|
|
$ignoreColumns = []; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$beanPropertyDescriptors = []; |
156
|
|
|
|
157
|
|
|
foreach ($table->getColumns() as $column) { |
158
|
|
|
if (array_search($column->getName(), $ignoreColumns) !== false) { |
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$fk = $this->isPartOfForeignKey($table, $column); |
163
|
|
|
if ($fk !== null) { |
164
|
|
|
// Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
165
|
|
|
foreach ($beanPropertyDescriptors as $beanDescriptor) { |
166
|
|
|
if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
167
|
|
|
continue 2; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
// Check that this property is not an inheritance relationship |
171
|
|
|
$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
172
|
|
|
if ($parentRelationship === $fk) { |
173
|
|
|
continue; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer); |
177
|
|
|
} else { |
178
|
|
|
$beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// Now, let's get the name of all properties and let's check there is no duplicate. |
183
|
|
|
/** @var $names AbstractBeanPropertyDescriptor[] */ |
184
|
|
|
$names = []; |
185
|
|
|
foreach ($beanPropertyDescriptors as $beanDescriptor) { |
186
|
|
|
$name = $beanDescriptor->getUpperCamelCaseName(); |
187
|
|
|
if (isset($names[$name])) { |
188
|
|
|
$names[$name]->useAlternativeName(); |
189
|
|
|
$beanDescriptor->useAlternativeName(); |
190
|
|
|
} else { |
191
|
|
|
$names[$name] = $beanDescriptor; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Final check (throw exceptions if problem arises) |
196
|
|
|
$names = []; |
197
|
|
|
foreach ($beanPropertyDescriptors as $beanDescriptor) { |
198
|
|
|
$name = $beanDescriptor->getUpperCamelCaseName(); |
199
|
|
|
if (isset($names[$name])) { |
200
|
|
|
throw new TDBMException("Unsolvable name conflict while generating method name"); |
201
|
|
|
} else { |
202
|
|
|
$names[$name] = $beanDescriptor; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// Last step, let's rebuild the list with a map: |
207
|
|
|
$beanPropertyDescriptorsMap = []; |
208
|
|
|
foreach ($beanPropertyDescriptors as $beanDescriptor) { |
209
|
|
|
$beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $beanPropertyDescriptorsMap; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function generateBeanConstructor() { |
216
|
|
|
$constructorProperties = $this->getConstructorProperties(); |
217
|
|
|
|
218
|
|
|
$constructorCode = " /** |
219
|
|
|
* The constructor takes all compulsory arguments. |
220
|
|
|
* |
221
|
|
|
%s |
222
|
|
|
*/ |
223
|
|
|
public function __construct(%s) { |
224
|
|
|
%s%s |
225
|
|
|
} |
226
|
|
|
"; |
227
|
|
|
|
228
|
|
|
$paramAnnotations = []; |
229
|
|
|
$arguments = []; |
230
|
|
|
$assigns = []; |
231
|
|
|
$parentConstructorArguments = []; |
232
|
|
|
|
233
|
|
|
foreach ($constructorProperties as $property) { |
234
|
|
|
$className = $property->getClassName(); |
235
|
|
|
if ($className) { |
|
|
|
|
236
|
|
|
$arguments[] = $className.' '.$property->getVariableName(); |
237
|
|
|
} else { |
238
|
|
|
$arguments[] = $property->getVariableName(); |
239
|
|
|
} |
240
|
|
|
$paramAnnotations[] = $property->getParamAnnotation(); |
241
|
|
|
if ($property->getTable()->getName() === $this->table->getName()) { |
242
|
|
|
$assigns[] = $property->getConstructorAssignCode(); |
243
|
|
|
} else { |
244
|
|
|
$parentConstructorArguments[] = $property->getVariableName(); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$parentConstrutorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
249
|
|
|
|
250
|
|
|
return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(", ", $arguments), $parentConstrutorCode, implode("\n", $assigns)); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function generateDirectForeignKeysCode() { |
254
|
|
|
$fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
255
|
|
|
|
256
|
|
|
$fksByTable = []; |
257
|
|
|
|
258
|
|
|
foreach ($fks as $fk) { |
259
|
|
|
$fksByTable[$fk->getLocalTableName()][] = $fk; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/* @var $fksByMethodName ForeignKeyConstraint[] */ |
|
|
|
|
263
|
|
|
$fksByMethodName = []; |
264
|
|
|
|
265
|
|
|
foreach ($fksByTable as $tableName => $fksForTable) { |
266
|
|
|
if (count($fksForTable) > 1) { |
267
|
|
|
foreach ($fksForTable as $fk) { |
268
|
|
|
$methodName = 'get'.TDBMDaoGenerator::toCamelCase($fk->getLocalTableName()).'By'; |
269
|
|
|
|
270
|
|
|
$camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $fk->getLocalColumns()); |
271
|
|
|
|
272
|
|
|
$methodName .= implode('And', $camelizedColumns); |
273
|
|
|
|
274
|
|
|
$fksByMethodName[$methodName] = $fk; |
275
|
|
|
} |
276
|
|
|
} else { |
277
|
|
|
$methodName = 'get'.TDBMDaoGenerator::toCamelCase($fksForTable[0]->getLocalTableName()); |
278
|
|
|
$fksByMethodName[$methodName] = $fk; |
|
|
|
|
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$code = ''; |
283
|
|
|
|
284
|
|
|
foreach ($fksByMethodName as $methodName => $fk) { |
285
|
|
|
$getterCode = ' /** |
286
|
|
|
* Returns the list of %s pointing to this bean via the %s column. |
287
|
|
|
* |
288
|
|
|
* @return %s[]|ResultIterator |
289
|
|
|
*/ |
290
|
|
|
public function %s() |
291
|
|
|
{ |
292
|
|
|
return $this->tdbmService->findObjects(%s, %s, %s); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
'; |
296
|
|
|
|
297
|
|
|
list($sql, $parametersCode) = $this->getFilters($fk); |
298
|
|
|
|
299
|
|
|
$beanClass = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
300
|
|
|
$code .= sprintf($getterCode, |
301
|
|
|
$beanClass, |
302
|
|
|
implode(', ', $fk->getColumns()), |
303
|
|
|
$beanClass, |
304
|
|
|
$methodName, |
305
|
|
|
var_export($fk->getLocalTableName(), true), |
306
|
|
|
$sql, |
307
|
|
|
$parametersCode |
308
|
|
|
); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return $code; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
private function getFilters(ForeignKeyConstraint $fk) { |
315
|
|
|
$sqlParts = []; |
316
|
|
|
$counter = 0; |
317
|
|
|
$parameters = []; |
318
|
|
|
|
319
|
|
|
$pkColumns = $this->table->getPrimaryKeyColumns(); |
320
|
|
|
|
321
|
|
|
foreach ($fk->getLocalColumns() as $columnName) { |
322
|
|
|
$paramName = "tdbmparam".$counter; |
323
|
|
|
$sqlParts[] = $fk->getLocalTableName().'.'.$columnName." = :".$paramName; |
324
|
|
|
|
325
|
|
|
$pkColumn = $pkColumns[$counter]; |
326
|
|
|
$parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($paramName, true), var_export($pkColumn, true), var_export($this->table->getName(), true)); |
327
|
|
|
$counter++; |
328
|
|
|
} |
329
|
|
|
$sql = "'".implode(' AND ', $sqlParts)."'"; |
330
|
|
|
$parametersCode = '[ '.implode(', ', $parameters).' ]'; |
331
|
|
|
|
332
|
|
|
return [$sql, $parametersCode]; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Generate code section about pivot tables |
337
|
|
|
* |
338
|
|
|
* @return string; |
|
|
|
|
339
|
|
|
*/ |
340
|
|
|
public function generatePivotTableCode() { |
341
|
|
|
|
342
|
|
|
$finalDescs = $this->getPivotTableDescriptors(); |
343
|
|
|
|
344
|
|
|
$code = ''; |
345
|
|
|
|
346
|
|
|
foreach ($finalDescs as $desc) { |
347
|
|
|
$code .= $this->getPivotTableCode($desc['name'], $desc['table'], $desc['localFK'], $desc['remoteFK']); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return $code; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
private function getPivotTableDescriptors() { |
354
|
|
|
$descs = []; |
355
|
|
|
foreach ($this->schemaAnalyzer->detectJunctionTables() as $table) { |
356
|
|
|
// There are exactly 2 FKs since this is a pivot table. |
357
|
|
|
$fks = array_values($table->getForeignKeys()); |
358
|
|
|
|
359
|
|
|
if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
360
|
|
|
$localFK = $fks[0]; |
361
|
|
|
$remoteFK = $fks[1]; |
362
|
|
|
} elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
363
|
|
|
$localFK = $fks[1]; |
364
|
|
|
$remoteFK = $fks[0]; |
365
|
|
|
} else { |
366
|
|
|
continue; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$descs[$remoteFK->getForeignTableName()][] = [ |
370
|
|
|
'table' => $table, |
371
|
|
|
'localFK' => $localFK, |
372
|
|
|
'remoteFK' => $remoteFK |
373
|
|
|
]; |
374
|
|
|
|
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
$finalDescs = []; |
378
|
|
|
foreach ($descs as $descArray) { |
379
|
|
|
if (count($descArray) > 1) { |
380
|
|
|
foreach ($descArray as $desc) { |
381
|
|
|
$desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName())."By".TDBMDaoGenerator::toCamelCase($desc['table']->getName()); |
382
|
|
|
$finalDescs[] = $desc; |
383
|
|
|
} |
384
|
|
|
} else { |
385
|
|
|
$desc = $descArray[0]; |
386
|
|
|
$desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()); |
387
|
|
|
$finalDescs[] = $desc; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $finalDescs; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
public function getPivotTableCode($name, Table $table, ForeignKeyConstraint $localFK, ForeignKeyConstraint $remoteFK) { |
|
|
|
|
395
|
|
|
$singularName = TDBMDaoGenerator::toSingular($name); |
396
|
|
|
$remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
397
|
|
|
$variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
398
|
|
|
|
399
|
|
|
$str = ' /** |
400
|
|
|
* Returns the list of %s associated to this bean via the %s pivot table. |
401
|
|
|
* |
402
|
|
|
* @return %s[] |
403
|
|
|
*/ |
404
|
|
|
public function get%s() { |
405
|
|
|
return $this->_getRelationships(%s); |
406
|
|
|
} |
407
|
|
|
'; |
408
|
|
|
|
409
|
|
|
$getterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $name, var_export($remoteFK->getLocalTableName(), true)); |
410
|
|
|
|
411
|
|
|
$str = ' /** |
412
|
|
|
* Adds a relationship with %s associated to this bean via the %s pivot table. |
413
|
|
|
* |
414
|
|
|
* @param %s %s |
415
|
|
|
*/ |
416
|
|
|
public function add%s(%s %s) { |
417
|
|
|
return $this->addRelationship(%s, %s); |
418
|
|
|
} |
419
|
|
|
'; |
420
|
|
|
|
421
|
|
|
$adderCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
422
|
|
|
|
423
|
|
|
$str = ' /** |
424
|
|
|
* Deletes the relationship with %s associated to this bean via the %s pivot table. |
425
|
|
|
* |
426
|
|
|
* @param %s %s |
427
|
|
|
*/ |
428
|
|
|
public function remove%s(%s %s) { |
429
|
|
|
return $this->_removeRelationship(%s, %s); |
430
|
|
|
} |
431
|
|
|
'; |
432
|
|
|
|
433
|
|
|
$removerCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
434
|
|
|
|
435
|
|
|
$str = ' /** |
436
|
|
|
* Returns whether this bean is associated with %s via the %s pivot table. |
437
|
|
|
* |
438
|
|
|
* @param %s %s |
439
|
|
|
* @return bool |
440
|
|
|
*/ |
441
|
|
|
public function has%s(%s %s) { |
442
|
|
|
return $this->hasRelationship(%s, %s); |
443
|
|
|
} |
444
|
|
|
'; |
445
|
|
|
|
446
|
|
|
$hasCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
447
|
|
|
|
448
|
|
|
|
449
|
|
|
$code = $getterCode.$adderCode.$removerCode.$hasCode; |
450
|
|
|
|
451
|
|
|
return $code; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
public function generateJsonSerialize() { |
455
|
|
|
$tableName = $this->table->getName(); |
456
|
|
|
$parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
457
|
|
|
if ($parentFk !== null) { |
458
|
|
|
$initializer = '$array = parent::jsonSerialize();'; |
459
|
|
|
} else { |
460
|
|
|
$initializer = '$array = [];'; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
$str = ' |
464
|
|
|
/** |
465
|
|
|
* Serializes the object for JSON encoding |
466
|
|
|
* |
467
|
|
|
* @param bool $stopRecursion Parameter used internally by TDBM to stop embedded objects from embedding other objects. |
468
|
|
|
* @return array |
469
|
|
|
*/ |
470
|
|
|
public function jsonSerialize($stopRecursion = false) |
471
|
|
|
{ |
472
|
|
|
%s |
473
|
|
|
%s |
474
|
|
|
%s |
475
|
|
|
return $array; |
476
|
|
|
} |
477
|
|
|
'; |
478
|
|
|
|
479
|
|
|
$propertiesCode = ''; |
480
|
|
|
foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
481
|
|
|
$propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
// Many to many relationships: |
485
|
|
|
|
486
|
|
|
$descs = $this->getPivotTableDescriptors(); |
487
|
|
|
|
488
|
|
|
$many2manyCode = ''; |
489
|
|
|
|
490
|
|
|
foreach ($descs as $desc) { |
491
|
|
|
$remoteFK = $desc['remoteFK']; |
492
|
|
|
$remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
493
|
|
|
$variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
494
|
|
|
|
495
|
|
|
$many2manyCode .= ' if (!$stopRecursion) { |
496
|
|
|
$array[\''.lcfirst($desc['name']).'\'] = array_map(function('.$remoteBeanName.' '.$variableName.') { |
497
|
|
|
return '.$variableName.'->jsonSerialize(true); |
498
|
|
|
}, $this->get'.$desc['name'].'()); |
499
|
|
|
} |
500
|
|
|
'; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
return sprintf($str, $initializer, $propertiesCode, $many2manyCode); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Writes the PHP bean file with all getters and setters from the table passed in parameter. |
508
|
|
|
* |
509
|
|
|
* @param string $beannamespace The namespace of the bean |
510
|
|
|
*/ |
511
|
|
|
public function generatePhpCode($beannamespace) { |
512
|
|
|
$baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($this->table->getName()); |
513
|
|
|
$className = TDBMDaoGenerator::getBeanNameFromTableName($this->table->getName()); |
514
|
|
|
$tableName = $this->table->getName(); |
515
|
|
|
|
516
|
|
|
$parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
517
|
|
|
if ($parentFk !== null) { |
518
|
|
|
$extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
519
|
|
|
$use = ""; |
520
|
|
|
} else { |
521
|
|
|
$extends = "AbstractTDBMObject"; |
522
|
|
|
$use = "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n\n"; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
$str = "<?php |
526
|
|
|
namespace {$beannamespace}; |
527
|
|
|
|
528
|
|
|
use Mouf\\Database\\TDBM\\ResultIterator; |
529
|
|
|
$use |
530
|
|
|
/* |
531
|
|
|
* This file has been automatically generated by TDBM. |
532
|
|
|
* DO NOT edit this file, as it might be overwritten. |
533
|
|
|
* If you need to perform changes, edit the $className class instead! |
534
|
|
|
*/ |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* The $baseClassName class maps the '$tableName' table in database. |
538
|
|
|
*/ |
539
|
|
|
class $baseClassName extends $extends implements \\JsonSerializable |
540
|
|
|
{ |
541
|
|
|
"; |
542
|
|
|
|
543
|
|
|
$str .= $this->generateBeanConstructor(); |
544
|
|
|
|
545
|
|
|
|
546
|
|
|
|
547
|
|
|
foreach ($this->getExposedProperties() as $property) { |
548
|
|
|
$str .= $property->getGetterSetterCode(); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
$str .= $this->generateDirectForeignKeysCode(); |
552
|
|
|
$str .= $this->generatePivotTableCode(); |
553
|
|
|
$str .= $this->generateJsonSerialize(); |
554
|
|
|
|
555
|
|
|
$str .= "} |
556
|
|
|
"; |
557
|
|
|
return $str; |
558
|
|
|
} |
559
|
|
|
} |
560
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.