Skip to content

Commit df282f4

Browse files
Add relations to entity form generation
1 parent 0f060e9 commit df282f4

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [v1.52.0](https://github.com/symfony/maker-bundle/releases/tag/v1.52.0)
4+
5+
### Feature
6+
7+
- [#1372](https://github.com/symfony/maker-bundle/issue/1372) - Support Entity relations in form generation - *@maelanleborgne*
8+
39
## [v1.50.0](https://github.com/symfony/maker-bundle/releases/tag/v1.50.0)
410

511
### Feature

src/Doctrine/EntityDetails.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\MakerBundle\Doctrine;
1313

1414
use Doctrine\Persistence\Mapping\ClassMetadata;
15+
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
1516

1617
/**
1718
* @author Sadicov Vladimir <[email protected]>
@@ -55,17 +56,21 @@ public function getFormFields(): array
5556
}
5657
}
5758

58-
foreach ($this->metadata->associationMappings as $fieldName => $relation) {
59-
if (\Doctrine\ORM\Mapping\ClassMetadata::ONE_TO_MANY !== $relation['type']) {
60-
$fields[] = $fieldName;
61-
}
62-
}
63-
6459
$fieldsWithTypes = [];
6560
foreach ($fields as $field) {
6661
$fieldsWithTypes[$field] = null;
6762
}
6863

64+
foreach ($this->metadata->associationMappings as $fieldName => $relation) {
65+
if (\Doctrine\ORM\Mapping\ClassMetadata::ONE_TO_MANY !== $relation['type']) {
66+
$fieldsWithTypes[$fieldName] = [
67+
'type' => EntityType::class,
68+
'options_code' => sprintf('\'class\' => %s::class,', $relation['targetEntity']),
69+
'extra_use_classes' => [$relation['targetEntity']],
70+
];
71+
}
72+
}
73+
6974
return $fieldsWithTypes;
7075
}
7176
}

src/Renderer/FormTypeRenderer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public function render(ClassNameDetails $formClassDetails, array $formFields, Cl
3939
if (isset($fieldTypeOptions['type'])) {
4040
$fieldTypeUseStatements[] = $fieldTypeOptions['type'];
4141
$fieldTypeOptions['type'] = Str::getShortClassName($fieldTypeOptions['type']);
42+
if (\array_key_exists('extra_use_classes', $fieldTypeOptions) && \count($fieldTypeOptions['extra_use_classes']) > 0) {
43+
$extraUseClasses = array_merge($extraUseClasses, $fieldTypeOptions['extra_use_classes'] ?? []);
44+
$fieldTypeOptions['options_code'] = str_replace(
45+
$fieldTypeOptions['extra_use_classes'],
46+
array_map(fn ($class) => Str::getShortClassName($class), $fieldTypeOptions['extra_use_classes']),
47+
$fieldTypeOptions['options_code']
48+
);
49+
}
4250
}
4351

4452
$fields[$name] = $fieldTypeOptions;

src/Resources/skeleton/form/Type.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
1616
->add('<?= $form_field ?>', <?= $typeOptions['type'] ?>::class)
1717
<?php else: ?>
1818
->add('<?= $form_field ?>', <?= $typeOptions['type'] ? ($typeOptions['type'].'::class') : 'null' ?>, [
19-
<?= $typeOptions['options_code']."\n" ?>
19+
<?= $typeOptions['options_code']."\n" ?>
2020
])
2121
<?php endif; ?>
2222
<?php endforeach; ?>

0 commit comments

Comments
 (0)