-
-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #1373 Add relations to entity form generation (maelanleborgne)
This PR was squashed before being merged into the 1.x-dev branch. Discussion ---------- Add relations to entity form generation PR for #1372 When generating a form for an entity (through `make:form` or `make:crud`), this feature adds an EntityType input for each compatible relations in the generate form. - [x] Add tests Commits ------- 85e4306 Add relations to entity form generation
- Loading branch information
Showing
17 changed files
with
735 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Bundle\MakerBundle\Doctrine; | ||
|
||
use Doctrine\Persistence\Mapping\ClassMetadata; | ||
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | ||
|
||
/** | ||
* @author Sadicov Vladimir <[email protected]> | ||
|
@@ -55,17 +56,25 @@ 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']) { | ||
continue; | ||
} | ||
$fieldsWithTypes[$fieldName] = [ | ||
'type' => EntityType::class, | ||
'options_code' => sprintf('\'class\' => %s::class,', $relation['targetEntity']).PHP_EOL.'\'choice_label\' => \'id\',', | ||
'extra_use_classes' => [$relation['targetEntity']], | ||
]; | ||
if (\Doctrine\ORM\Mapping\ClassMetadata::MANY_TO_MANY === $relation['type']) { | ||
$fieldsWithTypes[$fieldName]['options_code'] .= "\n'multiple' => true,"; | ||
} | ||
} | ||
|
||
return $fieldsWithTypes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class Book | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
#[ORM\Column()] | ||
private ?int $id = null; | ||
|
||
#[ORM\Column(name: 'title', length: 255)] | ||
private ?string $title = null; | ||
|
||
#[ORM\ManyToMany(targetEntity: Library::class, mappedBy: 'books')] | ||
private Collection $libraries; | ||
|
||
public function __construct() | ||
{ | ||
$this->libraries = new ArrayCollection(); | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function setTitle(string $title): static | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
|
||
public function getLibraries(): Collection | ||
{ | ||
return $this->libraries; | ||
} | ||
public function setLibraries(Collection $libraries): static | ||
{ | ||
$this->libraries = $libraries; | ||
return $this; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/fixtures/make-form/relation_many_to_many/Library.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class Library | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
#[ORM\Column()] | ||
private ?int $id = null; | ||
|
||
#[ORM\ManyToMany(targetEntity: Book::class, inversedBy: 'libraries')] | ||
private Collection $books; | ||
|
||
#[ORM\Column(name: 'name', length: 255)] | ||
private string $name; | ||
|
||
public function __construct() | ||
{ | ||
$this->books = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getBooks(): Collection | ||
{ | ||
return $this->books; | ||
} | ||
|
||
public function addBook(Book $book): static | ||
{ | ||
$this->books->add($book); | ||
return $this; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
public function setName(?string $name): static | ||
{ | ||
$this->name = $name; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class Author | ||
{ | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
#[ORM\Column()] | ||
private ?int $id = null; | ||
|
||
#[ORM\OneToMany(targetEntity: Book::class, mappedBy: 'library')] | ||
private Collection $books; | ||
|
||
#[ORM\Column(name: 'name', length: 255)] | ||
private string $name; | ||
|
||
public function __construct() | ||
{ | ||
$this->books = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getBooks(): Collection | ||
{ | ||
return $this->books; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
public function setName(?string $name): static | ||
{ | ||
$this->name = $name; | ||
return $this; | ||
} | ||
} |
Oops, something went wrong.