From f304685c6887a58a1236d48524724a67301dbef9 Mon Sep 17 00:00:00 2001 From: Maciej Kosiedowski Date: Mon, 22 May 2017 14:40:12 +0200 Subject: [PATCH 1/2] Fix #6460 - \Doctrine\ORM\Mapping\ClassMetadataInfo::hasField should return true for embedded fields --- .../ORM/Mapping/ClassMetadataInfo.php | 2 +- .../ORM/Functional/Ticket/DDC6460Test.php | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index f265814b1..75b528175 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1852,7 +1852,7 @@ class ClassMetadataInfo implements ClassMetadata */ public function hasField($fieldName) { - return isset($this->fieldMappings[$fieldName]); + return isset($this->fieldMappings[$fieldName]) || isset($this->embeddedClasses[$fieldName]); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php new file mode 100644 index 000000000..d50e2dbda --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php @@ -0,0 +1,100 @@ +_schemaTool->createSchema( + [ + $this->_em->getClassMetadata(DDC6460Entity::class), + $this->_em->getClassMetadata(DDC6460ParentEntity::class), + ] + ); + } catch (\Exception $e) { + } + } + + public function testInlineEmbeddable() + { + $isFieldMapped = $this->_em + ->getClassMetadata(DDC6460Entity::class) + ->hasField('embedded'); + + $this->assertTrue($isFieldMapped); + } + + public function testInlineEmbeddableProxyInitialization() + { + $entity = new DDC6460Entity(); + $entity->id = 1; + $entity->embedded = new DDC6460Embeddable(); + $entity->embedded->field = 'test'; + $this->_em->persist($entity); + $this->_em->flush(); + + $second = new DDC6460ParentEntity(); + $second->id = 1; + $second->lazyLoaded = $entity; + $this->_em->persist($second); + $this->_em->flush(); + + $this->_em->clear(); + + $proxy = $this->_em->getRepository(DDC6460ParentEntity::class)->findOneById(1); + + $this->assertNotNull($proxy->lazyLoaded->embedded); + } +} + +/** + * @Embeddable() + */ +class DDC6460Embeddable +{ + /** @Column(type="string") */ + public $field; +} + +/** + * @Entity() + */ +class DDC6460Entity +{ + /** + * @Id + * @GeneratedValue(strategy = "NONE") + * @Column(type = "integer") + */ + public $id; + + /** @Embedded(class = "DDC6460Embeddable") */ + public $embedded; +} + +/** + * @Entity() + */ +class DDC6460ParentEntity +{ + /** + * @Id + * @GeneratedValue(strategy = "NONE") + * @Column(type = "integer") + */ + public $id; + + /** @ManyToOne(targetEntity = "DDC6460Entity", fetch="EXTRA_LAZY") */ + public $lazyLoaded; +} From 76f0fe45af6568b6d862a7bd06f153f066495f2d Mon Sep 17 00:00:00 2001 From: Maciej Kosiedowski Date: Mon, 22 May 2017 15:52:19 +0200 Subject: [PATCH 2/2] fixes in tests --- .../ORM/Functional/Ticket/DDC6460Test.php | 27 +++++++++++++------ .../Tests/ORM/Mapping/ClassMetadataTest.php | 18 +++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php index d50e2dbda..9868c7d1c 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php @@ -2,12 +2,14 @@ namespace Doctrine\Tests\ORM\Functional\Ticket; +use Doctrine\DBAL\Schema\SchemaException; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Embeddable; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Id; use Doctrine\ORM\Mapping\GeneratedValue; use Doctrine\ORM\Mapping\ManyToOne; +use Doctrine\ORM\Proxy\Proxy; class DDC6460Test extends \Doctrine\Tests\OrmFunctionalTestCase { @@ -16,16 +18,19 @@ class DDC6460Test extends \Doctrine\Tests\OrmFunctionalTestCase parent::setUp(); try { - $this->_schemaTool->createSchema( + $this->setUpEntitySchema( [ - $this->_em->getClassMetadata(DDC6460Entity::class), - $this->_em->getClassMetadata(DDC6460ParentEntity::class), + DDC6460Entity::class, + DDC6460ParentEntity::class, ] ); - } catch (\Exception $e) { + } catch (SchemaException $e) { } } + /** + * @group DDC-6460 + */ public function testInlineEmbeddable() { $isFieldMapped = $this->_em @@ -35,6 +40,9 @@ class DDC6460Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertTrue($isFieldMapped); } + /** + * @group DDC-6460 + */ public function testInlineEmbeddableProxyInitialization() { $entity = new DDC6460Entity(); @@ -42,7 +50,6 @@ class DDC6460Test extends \Doctrine\Tests\OrmFunctionalTestCase $entity->embedded = new DDC6460Embeddable(); $entity->embedded->field = 'test'; $this->_em->persist($entity); - $this->_em->flush(); $second = new DDC6460ParentEntity(); $second->id = 1; @@ -52,9 +59,13 @@ class DDC6460Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); - $proxy = $this->_em->getRepository(DDC6460ParentEntity::class)->findOneById(1); + $secondEntityWithLazyParameter = $this->_em->getRepository(DDC6460ParentEntity::class)->findOneById(1); - $this->assertNotNull($proxy->lazyLoaded->embedded); + $this->assertInstanceOf(Proxy::class, $secondEntityWithLazyParameter->lazyLoaded); + $this->assertInstanceOf(DDC6460Entity::class, $secondEntityWithLazyParameter->lazyLoaded); + $this->assertFalse($secondEntityWithLazyParameter->lazyLoaded->__isInitialized()); + $this->assertEquals($secondEntityWithLazyParameter->lazyLoaded->embedded, $entity->embedded); + $this->assertTrue($secondEntityWithLazyParameter->lazyLoaded->__isInitialized()); } } @@ -95,6 +106,6 @@ class DDC6460ParentEntity */ public $id; - /** @ManyToOne(targetEntity = "DDC6460Entity", fetch="EXTRA_LAZY") */ + /** @ManyToOne(targetEntity = "DDC6460Entity", fetch="EXTRA_LAZY", cascade={"persist"}) */ public $lazyLoaded; } diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index 734ea8547..24d9559f7 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -1269,6 +1269,24 @@ class ClassMetadataTest extends OrmTestCase self::assertSame(['foo', 'baz'], $metadata->getColumnNames(['status', 'name'])); } + + /** + * @group DDC-6460 + */ + public function testInlineEmbeddable() + { + $classMetadata = new ClassMetadata(TestEntity1::class); + + $classMetadata->mapEmbedded( + [ + 'fieldName' => 'test', + 'class' => TestEntity1::class, + 'columnPrefix' => false, + ] + ); + + $this->assertTrue($classMetadata->hasField('test')); + } } /**