bug #23023 [DoctrineBridge][PropertyInfo] Added support for Doctrine Embeddables (vudaltsov)

This PR was squashed before being merged into the 2.8 branch (closes #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
-------

7816f3b7c7 [DoctrineBridge][PropertyInfo] Added support for Doctrine Embeddables
This commit is contained in:
Kévin Dunglas 2017-07-22 18:46:29 +02:00
commit 7428abad51
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
4 changed files with 115 additions and 1 deletions

View File

@ -50,7 +50,17 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
return;
}
return array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
$properties = array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
if ($metadata instanceof ClassMetadataInfo && class_exists('Doctrine\ORM\Mapping\Embedded') && $metadata->embeddedClasses) {
$properties = array_filter($properties, function ($property) {
return false === strpos($property, '.');
});
$properties = array_merge($properties, array_keys($metadata->embeddedClasses));
}
return $properties;
}
/**
@ -105,6 +115,10 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
));
}
if ($metadata instanceof ClassMetadataInfo && class_exists('Doctrine\ORM\Mapping\Embedded') && isset($metadata->embeddedClasses[$property])) {
return array(new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property]['class']));
}
if ($metadata->hasField($property)) {
$typeOfField = $metadata->getTypeOfField($property);
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);

View File

@ -64,6 +64,21 @@ class DoctrineExtractorTest extends TestCase
);
}
public function testGetPropertiesWithEmbedded()
{
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
}
$this->assertEquals(
array(
'id',
'embedded',
),
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded')
);
}
/**
* @dataProvider typesProvider
*/
@ -72,6 +87,27 @@ class DoctrineExtractorTest extends TestCase
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, array()));
}
public function testExtractWithEmbedded()
{
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
}
$expectedTypes = array(new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEmbeddable'
));
$actualTypes = $this->extractor->getTypes(
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded',
'embedded',
array()
);
$this->assertEquals($expectedTypes, $actualTypes);
}
public function typesProvider()
{
return array(

View File

@ -0,0 +1,28 @@
<?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\PropertyInfo\Fixtures;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Embeddable;
/**
* @Embeddable
*
* @author Udaltsov Valentin <udaltsov.valentin@gmail.com>
*/
class DoctrineEmbeddable
{
/**
* @Column(type="string")
*/
protected $field;
}

View File

@ -0,0 +1,36 @@
<?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\PropertyInfo\Fixtures;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Embedded;
/**
* @Entity
*
* @author Udaltsov Valentin <udaltsov.valentin@gmail.com>
*/
class DoctrineWithEmbedded
{
/**
* @Id
* @Column(type="smallint")
*/
public $id;
/**
* @Embedded(class="DoctrineEmbeddable")
*/
protected $embedded;
}