Skip to content

Commit

Permalink
Fix toOne translation fields (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 authored Feb 19, 2022
1 parent 619b982 commit 217da01
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/ObjectInfo/DoctrineORMInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace A2lix\AutoFormBundle\ObjectInfo;

use A2lix\AutoFormBundle\Form\Type\AutoFormType;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
Expand Down Expand Up @@ -62,19 +61,19 @@ private function getAssocsConfig(ClassMetadata $metadata, array $assocNames): ar
$assocsConfigs = [];

foreach ($assocNames as $assocName) {
if (!$metadata->isAssociationInverseSide($assocName)) {
$associationMapping = $metadata->getAssociationMapping($assocName);

if (isset($associationMapping['inversedBy'])) {
continue;
}

$class = $metadata->getAssociationTargetClass($assocName);

if ($metadata->isSingleValuedAssociation($assocName)) {
$nullable = ($metadata instanceof ClassMetadataInfo) && isset($metadata->discriminatorColumn['nullable']) && $metadata->discriminatorColumn['nullable'];

$assocsConfigs[$assocName] = [
'field_type' => AutoFormType::class,
'data_class' => $class,
'required' => !$nullable,
'required' => false,
];

continue;
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/Entity/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class Product
*/
protected $url;

/**
* @ORM\ManyToOne(targetEntity="Media")
*/
protected $mainMedia;

/**
* @ORM\OneToMany(targetEntity="Media", mappedBy="product", cascade={"all"}, orphanRemoval=true)
*/
Expand Down Expand Up @@ -95,6 +100,18 @@ public function setUrl(?string $url): self
return $this;
}

public function getMainMedia(): ?Media
{
return $this->mainMedia;
}

public function setMainMedia(?Media $mainMedia): self
{
$this->mainMedia = $mainMedia;

return $this;
}

public function getMedias(): Collection
{
return $this->medias;
Expand Down
15 changes: 14 additions & 1 deletion tests/Form/Type/AutoFormTypeAdvancedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public function testCreationFormWithOverriddenFieldsLabel(): Product
{
$form = $this->factory->createBuilder(AutoFormType::class, new Product(), [
'fields' => [
'mainMedia' => [
'label' => 'Main Media',
],
'url' => [
'label' => 'URL/URI',
],
Expand All @@ -46,15 +49,25 @@ public function testCreationFormWithOverriddenFieldsLabel(): Product
$media2->setUrl('http://example.org/media2')
->setDescription('media2 desc')
;
$media3 = new Media();
$media3->setUrl('http://example.org/media3')
->setDescription('media3 desc')
;

$product = new Product();
$product->setUrl('a2lix.fr')
$product
->setUrl('a2lix.fr')
->setMainMedia($media3)
->addMedia($media1)
->addMedia($media2)
;

$formData = [
'url' => 'a2lix.fr',
'mainMedia' => [
'url' => 'http://example.org/media3',
'description' => 'media3 desc',
],
'medias' => [
[
'url' => 'http://example.org/media1',
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Type/AutoFormTypeSimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testEmptyForm(): void
->getForm()
;

static::assertEquals(['create', 'title', 'description', 'url', 'medias'], array_keys($form->all()), 'Fields should matches Product fields');
static::assertEquals(['create', 'title', 'description', 'url', 'mainMedia', 'medias'], array_keys($form->all()), 'Fields should matches Product fields');

$mediasFormOptions = $form->get('medias')->getConfig()->getOptions();
static::assertEquals(AutoFormType::class, $mediasFormOptions['entry_type'], 'Media type should be an AutoType');
Expand Down

0 comments on commit 217da01

Please sign in to comment.