Skip to content

Commit

Permalink
Add relations to entity form generation
Browse files Browse the repository at this point in the history
  • Loading branch information
maelanleborgne committed Oct 13, 2023
1 parent 0f060e9 commit 3ab1276
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [v1.52.0](https://github.com/symfony/maker-bundle/releases/tag/v1.52.0)

### Feature

- [#1372](https://github.com/symfony/maker-bundle/issue/1372) - Support Entity relations in form generation - *@maelanleborgne*

## [v1.50.0](https://github.com/symfony/maker-bundle/releases/tag/v1.50.0)

### Feature
Expand Down
17 changes: 11 additions & 6 deletions src/Doctrine/EntityDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\MakerBundle\Doctrine;

use Doctrine\Persistence\Mapping\ClassMetadata;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

/**
* @author Sadicov Vladimir <sadikoff@gmail.com>
Expand Down Expand Up @@ -55,17 +56,21 @@ public function getFormFields(): array
}
}

foreach ($this->metadata->associationMappings as $fieldName => $relation) {
if (\Doctrine\ORM\Mapping\ClassMetadata::ONE_TO_MANY !== $relation['type']) {
$fields[] = $fieldName;
}
}

$fieldsWithTypes = [];
foreach ($fields as $field) {
$fieldsWithTypes[$field] = null;
}

foreach ($this->metadata->associationMappings as $fieldName => $relation) {
if (\Doctrine\ORM\Mapping\ClassMetadata::ONE_TO_MANY !== $relation['type']) {
$fieldsWithTypes[$fieldName] = [
'type' => EntityType::class,
'options_code' => sprintf('\'class\' => %s::class,', $relation['targetEntity']),
'extra_use_classes' => [$relation['targetEntity']],
];
}
}

return $fieldsWithTypes;
}
}
8 changes: 8 additions & 0 deletions src/Renderer/FormTypeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public function render(ClassNameDetails $formClassDetails, array $formFields, Cl
if (isset($fieldTypeOptions['type'])) {
$fieldTypeUseStatements[] = $fieldTypeOptions['type'];
$fieldTypeOptions['type'] = Str::getShortClassName($fieldTypeOptions['type']);
if (key_exists('extra_use_classes', $fieldTypeOptions) && \count($fieldTypeOptions['extra_use_classes']) > 0) {
$extraUseClasses = array_merge($extraUseClasses, $fieldTypeOptions['extra_use_classes'] ?? []);
$fieldTypeOptions['options_code'] = str_replace(
$fieldTypeOptions['extra_use_classes'],
array_map(fn ($class) => Str::getShortClassName($class), $fieldTypeOptions['extra_use_classes']),
$fieldTypeOptions['options_code']
);
}
}

$fields[$name] = $fieldTypeOptions;
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/skeleton/form/Type.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('<?= $form_field ?>', <?= $typeOptions['type'] ?>::class)
<?php else: ?>
->add('<?= $form_field ?>', <?= $typeOptions['type'] ? ($typeOptions['type'].'::class') : 'null' ?>, [
<?= $typeOptions['options_code']."\n" ?>
<?= $typeOptions['options_code']."\n" ?>
])
<?php endif; ?>
<?php endforeach; ?>
Expand Down

0 comments on commit 3ab1276

Please sign in to comment.