Skip to content

Commit

Permalink
bug #31953 [DoctrineBridge] fix handling nested embeddables (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.3 branch.

Discussion
----------

[DoctrineBridge] fix handling nested embeddables

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31911
| License       | MIT
| Doc PR        |

Commits
-------

37efa4b fix handling nested embeddables
  • Loading branch information
fabpot committed Jun 11, 2019
2 parents 2b8e441 + 37efa4b commit cf728f5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Expand Up @@ -22,4 +22,9 @@ class DoctrineLoaderEmbed
* @ORM\Column(length=25)
*/
public $embeddedMaxLength;

/**
* @ORM\Embedded(class=DoctrineLoaderNestedEmbed::class)
*/
public $nestedEmbedded;
}
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Embeddable()
*/
class DoctrineLoaderNestedEmbed
{
/**
* @ORM\Column(length=27)
*/
public $nestedEmbeddedMaxLength;
}
15 changes: 15 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php
Expand Up @@ -16,6 +16,7 @@
use Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEmbed;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderNestedEmbed;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderParentEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
Expand Down Expand Up @@ -109,6 +110,20 @@ public function testLoadClassMetadata()
$this->assertInstanceOf(Length::class, $embeddedMaxLengthConstraints[0]);
$this->assertSame(25, $embeddedMaxLengthConstraints[0]->max);

$nestedEmbeddedMetadata = $embeddedClassMetadata->getPropertyMetadata('nestedEmbedded');
$this->assertCount(1, $nestedEmbeddedMetadata);
$this->assertSame(CascadingStrategy::CASCADE, $nestedEmbeddedMetadata[0]->getCascadingStrategy());
$this->assertSame(TraversalStrategy::IMPLICIT, $nestedEmbeddedMetadata[0]->getTraversalStrategy());

$nestedEmbeddedClassMetadata = $validator->getMetadataFor(new DoctrineLoaderNestedEmbed());

$nestedEmbeddedMaxLengthMetadata = $nestedEmbeddedClassMetadata->getPropertyMetadata('nestedEmbeddedMaxLength');
$this->assertCount(1, $nestedEmbeddedMaxLengthMetadata);
$nestedEmbeddedMaxLengthConstraints = $nestedEmbeddedMaxLengthMetadata[0]->getConstraints();
$this->assertCount(1, $nestedEmbeddedMaxLengthConstraints);
$this->assertInstanceOf(Length::class, $nestedEmbeddedMaxLengthConstraints[0]);
$this->assertSame(27, $nestedEmbeddedMaxLengthConstraints[0]->max);

$this->assertCount(0, $classMetadata->getPropertyMetadata('guidField'));
$this->assertCount(0, $classMetadata->getPropertyMetadata('simpleArrayField'));

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
Expand Up @@ -79,7 +79,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool

$constraint = $this->getLengthConstraint($metadata, $mapping['fieldName']);
if (null === $constraint) {
if (isset($mapping['originalClass'])) {
if (isset($mapping['originalClass']) && false === strpos($mapping['declaredField'], '.')) {
$metadata->addPropertyConstraint($mapping['declaredField'], new Valid());
} elseif (property_exists($className, $mapping['fieldName'])) {
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
Expand Down

0 comments on commit cf728f5

Please sign in to comment.