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

Fixes #5755 - Parent class fields are not cloned #5768

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
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private function createCloner(ClassMetadata $classMetadata, EntityPersister $ent
);
}

foreach ($class->getReflectionClass()->getProperties() as $property) {
foreach ($class->getReflectionProperties() as $property) {
if ( ! $class->hasField($property->name) && ! $class->hasAssociation($property->name)) {
continue;
}
Expand Down
51 changes: 45 additions & 6 deletions tests/Doctrine/Tests/ORM/Proxy/ProxyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Doctrine\Tests\ORM\Proxy;

use Doctrine\Common\Proxy\AbstractProxyFactory;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\Common\Proxy\ProxyGenerator;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\DriverMock;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Mocks\UnitOfWorkMock;
use Doctrine\Tests\Mocks\DriverMock;
use Doctrine\Common\Proxy\AbstractProxyFactory;
use Doctrine\Tests\Models\Company\CompanyEmployee;

/**
* Test the proxy generator. Its work is generating on-the-fly subclasses of a given model, which implement the Proxy pattern.
Expand Down Expand Up @@ -62,9 +62,9 @@ public function testReferenceProxyDelegatesLoadingToThePersister()

$persister
->expects($this->atLeastOnce())
->method('load')
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass))
->will($this->returnValue(new \stdClass()));
->method('load')
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass))
->will($this->returnValue(new \stdClass()));

$proxy->getDescription();
}
Expand Down Expand Up @@ -136,6 +136,45 @@ public function testFailedProxyCloningDoesNotMarkTheProxyAsInitialized()
$this->assertInstanceOf('Closure', $proxy->__getInitializer(), 'The initializer wasn\'t removed');
$this->assertInstanceOf('Closure', $proxy->__getCloner(), 'The cloner wasn\'t removed');
}

public function testProxyClonesParentFields()
{
$companyEmployee = new CompanyEmployee();
$companyEmployee->setSalary(1000); // A property on the CompanyEmployee
$companyEmployee->setName("Bob"); // A property on the parent class, CompanyPerson

// Set the id of the CompanyEmployee (which is in the parent CompanyPerson)
$class = new \ReflectionClass('Doctrine\Tests\Models\Company\CompanyPerson');

$property = $class->getProperty('id');
$property->setAccessible(true);

$property->setValue($companyEmployee, 42);

$classMetaData = $this->emMock->getClassMetadata('Doctrine\Tests\Models\Company\CompanyEmployee');

$persister = $this->getMock('Doctrine\ORM\Persisters\Entity\BasicEntityPersister', array('load', 'getClassMetadata'), array(), '', false);
$this->uowMock->setEntityPersister('Doctrine\Tests\Models\Company\CompanyEmployee', $persister);

/* @var $proxy \Doctrine\Common\Proxy\Proxy */
$proxy = $this->proxyFactory->getProxy('Doctrine\Tests\Models\Company\CompanyEmployee', array('id' => 42));

$persister
->expects($this->atLeastOnce())
->method('load')
->will($this->returnValue($companyEmployee));

$persister
->expects($this->atLeastOnce())
->method('getClassMetadata')
->will($this->returnValue($classMetaData));

$cloned = clone $proxy;

$this->assertEquals(42, $cloned->getId(), "Expected the Id to be cloned");
$this->assertEquals(1000, $cloned->getSalary(), "Expect properties on the CompanyEmployee class to be cloned");
$this->assertEquals("Bob", $cloned->getName(), "Expect properties on the CompanyPerson class to be cloned");
}
}

abstract class AbstractClass
Expand Down