1
0
mirror of synced 2025-02-02 21:41:45 +03:00

Allow the usage of embedded objects on parent classes.

The `ClassMetadataInfo` was always using the "current class" to
fetch the reflection of a property even when a field is declared
on the parent class (which causes `ReflectionProperty` to throw
an exception).
This commit is contained in:
Luís Cobucci 2016-06-09 19:46:37 +00:00 committed by Marco Pivetta
parent b59b966cc2
commit 27f3bc1e2c
5 changed files with 199 additions and 2 deletions

View File

@ -941,8 +941,13 @@ class ClassMetadataInfo implements ClassMetadata
continue;
}
$parentReflFields[$property] = $reflService->getAccessibleProperty($this->name, $property);
$this->reflFields[$property] = $reflService->getAccessibleProperty($this->name, $property);
$fieldRefl = $reflService->getAccessibleProperty(
isset($embeddedClass['declared']) ? $embeddedClass['declared'] : $this->name,
$property
);
$parentReflFields[$property] = $fieldRefl;
$this->reflFields[$property] = $fieldRefl;
}
foreach ($this->fieldMappings as $field => $mapping) {

View File

@ -0,0 +1,60 @@
<?php
namespace Doctrine\Tests\Models\DDC3303;
/**
* @Embeddable
*/
class DDC3303Address
{
/**
* @Column(type="string")
*
* @var string
*/
private $street;
/**
* @Column(type="integer")
*
* @var int
*/
private $number;
/**
* @Column(type="string")
*
* @var string
*/
private $city;
public function __construct($street, $number, $city)
{
$this->street = $street;
$this->number = $number;
$this->city = $city;
}
/**
* @return string
*/
public function getStreet()
{
return $this->street;
}
/**
* @return mixed
*/
public function getNumber()
{
return $this->number;
}
/**
* @return string
*/
public function getCity()
{
return $this->city;
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace Doctrine\Tests\Models\DDC3303;
/**
* @Entity
* @Table(name="ddc3303_employee")
*/
class DDC3303Employee extends DDC3303Person
{
/**
* @Column(type="string")
*
* @var string
*/
private $company;
public function __construct($name, DDC3303Address $address, $company)
{
parent::__construct($name, $address);
$this->company = $company;
}
/**
* @return string
*/
public function getCompany()
{
return $this->company;
}
/**
* @param string $company
*/
public function setCompany($company)
{
$this->company = $company;
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace Doctrine\Tests\Models\DDC3303;
/**
* @MappedSuperclass
*/
abstract class DDC3303Person
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*
* @var int
*/
private $id;
/**
* @Column(type="string")
*
* @var string
*/
private $name;
/**
* @Embedded(class="DDC3303Address")
*
* @var DDC3303Address
*/
private $address;
public function __construct($name, DDC3303Address $address)
{
$this->name = $name;
$this->address = $address;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return DDC3303Address
*/
public function getAddress()
{
return $this->address;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\Models\DDC3303\DDC3303Address;
use Doctrine\Tests\Models\DDC3303\DDC3303Employee;
use Doctrine\Tests\OrmFunctionalTestCase;
class DDC3303Test extends OrmFunctionalTestCase
{
/**
* @before
*/
public function createSchema()
{
$this->_schemaTool->createSchema(array($this->_em->getClassMetadata(DDC3303Employee::class)));
}
public function testEmbeddedObjectsAreAlsoInherited()
{
$employee = new DDC3303Employee(
'John Doe',
new DDC3303Address('Somewhere', 123, 'Over the rainbow'),
'Doctrine Inc'
);
$this->_em->persist($employee);
$this->_em->flush();
$this->_em->clear();
$this->assertEquals($employee, $this->_em->find(DDC3303Employee::class, 1));
}
}