|
32 | 32 |
|
33 | 33 | /** |
34 | 34 | * @phpstan-import-type FieldMapping from ClassMetadataInfo |
| 35 | + * @phpstan-import-type EmbeddedClassMapping from ClassMetadataInfo |
35 | 36 | * @phpstan-import-type AssociationMapping from ClassMetadataInfo |
36 | 37 | * @phpstan-import-type FileParts from GenerateEntityRequest |
37 | 38 | */ |
@@ -111,7 +112,11 @@ public function generate(string $className): void |
111 | 112 | } |
112 | 113 |
|
113 | 114 | if ($metadata->hasField($property)) { |
114 | | - $this->addField($request, $metadata->fieldMappings[$property]); |
| 115 | + if (\array_key_exists($property, $metadata->fieldMappings)) { |
| 116 | + $this->addField($request, $metadata->fieldMappings[$property]); |
| 117 | + } else { |
| 118 | + $this->addEmbedded($request, $property, $metadata->embeddedClasses[$property]); |
| 119 | + } |
115 | 120 | } elseif ($metadata->hasAssociation($property)) { |
116 | 121 | $this->addAssociation($request, $metadata->associationMappings[$property]); |
117 | 122 | } |
@@ -300,6 +305,47 @@ protected function addField(GenerateEntityRequest $request, array $fieldMapping) |
300 | 305 | } |
301 | 306 | } |
302 | 307 |
|
| 308 | + /** |
| 309 | + * @param EmbeddedClassMapping $embeddedMapping |
| 310 | + */ |
| 311 | + protected function addEmbedded(GenerateEntityRequest $request, string $fieldName, array $embeddedMapping): void |
| 312 | + { |
| 313 | + $targetClass = $embeddedMapping['class']; |
| 314 | + $phpType = (new \ReflectionProperty($request->reflectionClass->getName(), $fieldName))->getType(); |
| 315 | + |
| 316 | + $targetClassAlias = $request->useStatementManipulator->addUseStatementIfNecessary($targetClass); |
| 317 | + if ($request->reflectionClass->getName() === $targetClass) { |
| 318 | + $targetClassAlias = 'self'; |
| 319 | + } |
| 320 | + |
| 321 | + $setMethodName = $this->buildMethodName(self::TYPE_SET, $fieldName); |
| 322 | + if (!$this->methodIsDefinedOutsideBlock($request, $setMethodName)) { |
| 323 | + $request->newBlockContents[] = $this->renderBlock($request->reflectionClass, 'embedded_set', [ |
| 324 | + 'methodName' => $setMethodName, |
| 325 | + 'fieldName' => $fieldName, |
| 326 | + 'variableName' => $this->buildVariableName(self::TYPE_SET, $fieldName), |
| 327 | + 'targetClass' => $targetClass, |
| 328 | + 'targetClassAlias' => $targetClassAlias, |
| 329 | + 'phpType' => $phpType, |
| 330 | + 'request' => $request, |
| 331 | + 'embeddedMapping' => $embeddedMapping, |
| 332 | + ]); |
| 333 | + } |
| 334 | + |
| 335 | + $getMethodName = $this->buildMethodName(self::TYPE_GET, $fieldName); |
| 336 | + if (!$this->methodIsDefinedOutsideBlock($request, $getMethodName)) { |
| 337 | + $request->newBlockContents[] = $this->renderBlock($request->reflectionClass, 'embedded_get', [ |
| 338 | + 'methodName' => $getMethodName, |
| 339 | + 'fieldName' => $fieldName, |
| 340 | + 'targetClass' => $targetClass, |
| 341 | + 'targetClassAlias' => $targetClassAlias, |
| 342 | + 'phpType' => $phpType, |
| 343 | + 'request' => $request, |
| 344 | + 'embeddedMapping' => $embeddedMapping, |
| 345 | + ]); |
| 346 | + } |
| 347 | + } |
| 348 | + |
303 | 349 | /** |
304 | 350 | * @param AssociationMapping $associationMapping |
305 | 351 | */ |
|
0 commit comments