8000 don't add embedded properties to wrapping class metadata · symfony/symfony@56fe025 · GitHub
[go: up one dir, main page]

Skip to content

Commit 56fe025

Browse files
committed
don't add embedded properties to wrapping class metadata
1 parent d8224b8 commit 56fe025

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping as ORM;
15+
16+
/**
17+
* @ORM\Embeddable
18+
*/
19+
class DoctrineLoaderEmbed
20+
{
21+
/**
22+
* @ORM\Column(length=25)
23+
*/
24+
public $embeddedMaxLength;
25+
}

src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderEntity.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ class DoctrineLoaderEntity
5555
* @ORM\Column(unique=true)
5656
*/
5757
public $alreadyMappedUnique;
58+
59+
/**
60+
* @ORM\Embedded(class=DoctrineLoaderEmbed::class)
61+
*/
62+
public $embedded;
5863
}

src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
1616
use Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser;
17+
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEmbed;
1718
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEntity;
1819
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1920
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
2021
use Symfony\Component\Validator\Constraints\Length;
22+
use Symfony\Component\Validator\Mapping\CascadingStrategy;
2123
use Symfony\Component\Validator\Mapping\ClassMetadata;
24+
use Symfony\Component\Validator\Mapping\TraversalStrategy;
2225
use Symfony\Component\Validator\Tests\Fixtures\Entity;
2326
use Symfony\Component\Validator\Validation;
2427
use Symfony\Component\Validator\ValidatorBuilder;
@@ -36,7 +39,7 @@ public function testLoadClassMetadata()
3639

3740
$validator = Validation::createValidatorBuilder()
3841
->enableAnnotationMapping()
39-
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoaderEntity$}'))
42+
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
4043
->getValidator()
4144
;
4245

@@ -71,6 +74,20 @@ public function testLoadClassMetadata()
7174
$this->assertInstanceOf(Length::class, $alreadyMappedMaxLengthConstraints[0]);
7275
$this->assertSame(10, $alreadyMappedMaxLengthConstraints[0]->max);
7376
$this->assertSame(1, $alreadyMappedMaxLengthConstraints[0]->min);
77+
78+
$embeddedMetadata = $classMetadata->getPropertyMetadata('embedded');
79+
$this->assertCount(1, $embeddedMetadata);
80+
$this->assertSame(CascadingStrategy::CASCADE, $embeddedMetadata[0]->getCascadingStrategy());
81+
$this->assertSame(TraversalStrategy::IMPLICIT, $embeddedMetadata[0]->getTraversalStrategy());
82+
83+
$embeddedClassMetadata = $validator->getMetadataFor(new DoctrineLoaderEmbed());
84+
85+
$embeddedMaxLengthMetadata = $embeddedClassMetadata->getPropertyMetadata('embeddedMaxLength');
86+
$this->assertCount(1, $embeddedMaxLengthMetadata);
87+
$embeddedMaxLengthConstraints = $embeddedMaxLengthMetadata[0]->getConstraints();
88+
$this->assertCount(1, $embeddedMaxLengthConstraints);
89+
$this->assertInstanceOf(Length::class, $embeddedMaxLengthConstraints[0]);
90+
$this->assertSame(25, $embeddedMaxLengthConstraints[0]->max);
7491
}
7592

7693
public function testFieldMappingsConfiguration()

src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
1818
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1919
use Symfony\Component\Validator\Constraints\Length;
20+
use Symfony\Component\Validator\Constraints\Valid;
2021
use Symfony\Component\Validator\Mapping\ClassMetadata;
2122
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
2223

@@ -78,7 +79,11 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
7879

7980
$constraint = $this->getLengthConstraint($metadata, $mapping['fieldName']);
8081
if (null === $constraint) {
81-
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
82+
if (isset($mapping['originalClass'])) {
83+
$metadata->addPropertyConstraint($mapping['declaredField'], new Valid());
84+
} else {
85+
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
86+
}
8287
} elseif (null === $constraint->max) {
8388
// If a Length constraint exists and no max length has been explicitly defined, set it
8489
$constraint->max = $mapping['length'];

0 commit comments

Comments
 (0)
0