[DoctrineBridge][PropertyInfo] Added support for Doctrine Embeddables
This commit is contained in:
parent
d12f26992b
commit
7816f3b7c7
@ -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);
|
||||
|
@ -62,6 +62,21 @@ class DoctrineExtractorTest extends \PHPUnit_Framework_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
|
||||
*/
|
||||
@ -70,6 +85,27 @@ class DoctrineExtractorTest extends \PHPUnit_Framework_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(
|
||||
|
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
Reference in New Issue
Block a user