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

Allow the usage of embedded objects on parent classes. #5867

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,13 @@ public function wakeupReflection($reflService)
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) {
Expand Down
60 changes: 60 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3303/DDC3303Address.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3303/DDC3303Employee.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
61 changes: 61 additions & 0 deletions tests/Doctrine/Tests/Models/DDC3303/DDC3303Person.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
32 changes: 32 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3303Test.php
Original file line number Diff line number Diff line change
@@ -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));
}
}