bug #31953 [DoctrineBridge] fix handling nested embeddables (xabbuh)

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
-------

37efa4bb8c fix handling nested embeddables
This commit is contained in:
Fabien Potencier 2019-06-11 08:20:42 +02:00
commit cf728f5da2
4 changed files with 46 additions and 1 deletions

View File

@ -22,4 +22,9 @@ class DoctrineLoaderEmbed
* @ORM\Column(length=25)
*/
public $embeddedMaxLength;
/**
* @ORM\Embedded(class=DoctrineLoaderNestedEmbed::class)
*/
public $nestedEmbedded;
}

View File

@ -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;
}

View File

@ -16,6 +16,7 @@ use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
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;
@ -109,6 +110,20 @@ class DoctrineLoaderTest extends TestCase
$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'));

View File

@ -79,7 +79,7 @@ final class DoctrineLoader implements LoaderInterface
$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']]));