Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #6460 - \Doctrine\ORM\Mapping\ClassMetadataInfo::hasField should return true for embedded fields #6462

Merged
merged 2 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ public function getIdentifier()
*/
public function hasField($fieldName)
{
return isset($this->fieldMappings[$fieldName]);
return isset($this->fieldMappings[$fieldName]) || isset($this->embeddedClasses[$fieldName]);
}

/**
Expand Down
100 changes: 100 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC6460Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

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;

class DDC6460Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
parent::setUp();

try {
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(DDC6460Entity::class),
$this->_em->getClassMetadata(DDC6460ParentEntity::class),
]
);
} catch (\Exception $e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please only catch schemaexception

}
}

public function testInlineEmbeddable()
{
$isFieldMapped = $this->_em
->getClassMetadata(DDC6460Entity::class)
->hasField('embedded');

$this->assertTrue($isFieldMapped);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should probably be on the class metadata tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added similar test there

}

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why two flush operations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added cascade persist in order to remove one flush

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


$this->_em->clear();

$proxy = $this->_em->getRepository(DDC6460ParentEntity::class)->findOneById(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a proxy here: please add assertions to verify if it is a proxy, and if it is loaded


$this->assertNotNull($proxy->lazyLoaded->embedded);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use self::assertInstanceOf()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also check the value of field after this call

}
}

/**
* @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;
}