bug #31811 [DoctrineBridge] don't add embedded properties to wrapping class metadata (xabbuh)

This PR was merged into the 4.3 branch.

Discussion
----------

[DoctrineBridge] don't add embedded properties to wrapping class metadata

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

Commits
-------

56fe02512c don't add embedded properties to wrapping class metadata
This commit is contained in:
Fabien Potencier 2019-06-04 08:16:02 +02:00
commit 66b87abcfc
4 changed files with 54 additions and 2 deletions

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 DoctrineLoaderEmbed
{
/**
* @ORM\Column(length=25)
*/
public $embeddedMaxLength;
}

View File

@ -55,4 +55,9 @@ class DoctrineLoaderEntity
* @ORM\Column(unique=true)
*/
public $alreadyMappedUnique;
/**
* @ORM\Embedded(class=DoctrineLoaderEmbed::class)
*/
public $embedded;
}

View File

@ -14,11 +14,14 @@ namespace Symfony\Bridge\Doctrine\Tests\Validator;
use PHPUnit\Framework\TestCase;
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\Validator\Constraints\UniqueEntity;
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Mapping\CascadingStrategy;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\TraversalStrategy;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\ValidatorBuilder;
@ -36,7 +39,7 @@ class DoctrineLoaderTest extends TestCase
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoaderEntity$}'))
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
->getValidator()
;
@ -71,6 +74,20 @@ class DoctrineLoaderTest extends TestCase
$this->assertInstanceOf(Length::class, $alreadyMappedMaxLengthConstraints[0]);
$this->assertSame(10, $alreadyMappedMaxLengthConstraints[0]->max);
$this->assertSame(1, $alreadyMappedMaxLengthConstraints[0]->min);
$embeddedMetadata = $classMetadata->getPropertyMetadata('embedded');
$this->assertCount(1, $embeddedMetadata);
$this->assertSame(CascadingStrategy::CASCADE, $embeddedMetadata[0]->getCascadingStrategy());
$this->assertSame(TraversalStrategy::IMPLICIT, $embeddedMetadata[0]->getTraversalStrategy());
$embeddedClassMetadata = $validator->getMetadataFor(new DoctrineLoaderEmbed());
$embeddedMaxLengthMetadata = $embeddedClassMetadata->getPropertyMetadata('embeddedMaxLength');
$this->assertCount(1, $embeddedMaxLengthMetadata);
$embeddedMaxLengthConstraints = $embeddedMaxLengthMetadata[0]->getConstraints();
$this->assertCount(1, $embeddedMaxLengthConstraints);
$this->assertInstanceOf(Length::class, $embeddedMaxLengthConstraints[0]);
$this->assertSame(25, $embeddedMaxLengthConstraints[0]->max);
}
public function testFieldMappingsConfiguration()

View File

@ -17,6 +17,7 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
@ -78,7 +79,11 @@ final class DoctrineLoader implements LoaderInterface
$constraint = $this->getLengthConstraint($metadata, $mapping['fieldName']);
if (null === $constraint) {
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
if (isset($mapping['originalClass'])) {
$metadata->addPropertyConstraint($mapping['declaredField'], new Valid());
} else {
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
}
} elseif (null === $constraint->max) {
// If a Length constraint exists and no max length has been explicitly defined, set it
$constraint->max = $mapping['length'];