Skip to content

Commit 7428aba

Browse files
committed
bug symfony#23023 [DoctrineBridge][PropertyInfo] Added support for Doctrine Embeddables (vudaltsov)
This PR was squashed before being merged into the 2.8 branch (closes symfony#23023). Discussion ---------- [DoctrineBridge][PropertyInfo] Added support for Doctrine Embeddables | Q | A | ------------- | --- | Branch? | 2.8 and higher | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Note that [Embeddables appeared only in doctrine/orm 2.5](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/changelog/migration_2_5.html). I added class_exists checks for that. Commits ------- 7816f3b [DoctrineBridge][PropertyInfo] Added support for Doctrine Embeddables
2 parents 6158ab3 + 7816f3b commit 7428aba

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ public function getProperties($class, array $context = array())
5050
return;
5151
}
5252

53-
return array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
53+
$properties = array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
54+
55+
if ($metadata instanceof ClassMetadataInfo && class_exists('Doctrine\ORM\Mapping\Embedded') && $metadata->embeddedClasses) {
56+
$properties = array_filter($properties, function ($property) {
57+
return false === strpos($property, '.');
58+
});
59+
60+
$properties = array_merge($properties, array_keys($metadata->embeddedClasses));
61+
}
62+
63+
return $properties;
5464
}
5565

5666
/**
@@ -105,6 +115,10 @@ public function getTypes($class, $property, array $context = array())
105115
));
106116
}
107117

118+
if ($metadata instanceof ClassMetadataInfo && class_exists('Doctrine\ORM\Mapping\Embedded') && isset($metadata->embeddedClasses[$property])) {
119+
return array(new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property]['class']));
120+
}
121+
108122
if ($metadata->hasField($property)) {
109123
$typeOfField = $metadata->getTypeOfField($property);
110124
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public function testGetProperties()
6464
);
6565
}
6666

67+
public function testGetPropertiesWithEmbedded()
68+
{
69+
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
70+
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
71+
}
72+
73+
$this->assertEquals(
74+
array(
75+
'id',
76+
'embedded',
77+
),
78+
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded')
79+
);
80+
}
81+
6782
/**
6883
* @dataProvider typesProvider
6984
*/
@@ -72,6 +87,27 @@ public function testExtract($property, array $type = null)
7287
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, array()));
7388
}
7489

90+
public function testExtractWithEmbedded()
91+
{
92+
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
93+
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
94+
}
95+
96+
$expectedTypes = array(new Type(
97+
Type::BUILTIN_TYPE_OBJECT,
98+
false,
99+
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEmbeddable'
100+
));
101+
102+
$actualTypes = $this->extractor->getTypes(
103+
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded',
104+
'embedded',
105+
array()
106+
);
107+
108+
$this->assertEquals($expectedTypes, $actualTypes);
109+
}
110+
75111
public function typesProvider()
76112
{
77113
return array(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\PropertyInfo\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Embeddable;
16+
17+
/**
18+
* @Embeddable
19+
*
20+
* @author Udaltsov Valentin <udaltsov.valentin@gmail.com>
21+
*/
22+
class DoctrineEmbeddable
23+
{
24+
/**
25+
* @Column(type="string")
26+
*/
27+
protected $field;
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\PropertyInfo\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
use Doctrine\ORM\Mapping\Id;
17+
use Doctrine\ORM\Mapping\Embedded;
18+
19+
/**
20+
* @Entity
21+
*
22+
* @author Udaltsov Valentin <udaltsov.valentin@gmail.com>
23+
*/
24+
class DoctrineWithEmbedded
25+
{
26+
/**
27+
* @Id
28+
* @Column(type="smallint")
29+
*/
30+
public $id;
31+
32+
/**
33+
* @Embedded(class="DoctrineEmbeddable")
34+
*/
35+
protected $embedded;
36+
}

0 commit comments

Comments
 (0)