Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jun 4, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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;
}
@@ -55,4 +55,9 @@ class DoctrineLoaderEntity
* @ORM\Column(unique=true)
*/
public $alreadyMappedUnique;
/**
* @ORM\Embedded(class=DoctrineLoaderEmbed::class)
*/
public $embedded;
}
@@ -14,11 +14,14 @@
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 @@ public function testLoadClassMetadata()
$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 @@ public function testLoadClassMetadata()
$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()
@@ -17,6 +17,7 @@
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 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
$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'];
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.