Merge pull request #148 from asm89/ProxyIdentifer
Do not load entity on retrieving identifier from a proxy
This commit is contained in:
commit
ba38f3e1e9
6 changed files with 64 additions and 6 deletions
|
@ -209,6 +209,11 @@ class ProxyFactory
|
||||||
|
|
||||||
$methods .= $parameterString . ')';
|
$methods .= $parameterString . ')';
|
||||||
$methods .= "\n" . ' {' . "\n";
|
$methods .= "\n" . ' {' . "\n";
|
||||||
|
if ($this->isShortIdentifierGetter($method, $class)) {
|
||||||
|
$methods .= ' if ($this->__isInitialized__ === false) {' . "\n";
|
||||||
|
$methods .= ' return $this->_identifier["' . lcfirst(substr($method->getName(), 3)) . '"];' . "\n";
|
||||||
|
$methods .= ' }' . "\n";
|
||||||
|
}
|
||||||
$methods .= ' $this->__load();' . "\n";
|
$methods .= ' $this->__load();' . "\n";
|
||||||
$methods .= ' return parent::' . $method->getName() . '(' . $argumentString . ');';
|
$methods .= ' return parent::' . $method->getName() . '(' . $argumentString . ');';
|
||||||
$methods .= "\n" . ' }' . "\n";
|
$methods .= "\n" . ' }' . "\n";
|
||||||
|
@ -218,6 +223,21 @@ class ProxyFactory
|
||||||
return $methods;
|
return $methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ReflectionMethod $method
|
||||||
|
* @param ClassMetadata $class
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isShortIdentifierGetter($method, $class)
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
$method->getNumberOfParameters() == 0 &&
|
||||||
|
substr($method->getName(), 0, 3) == "get" &&
|
||||||
|
in_array(lcfirst(substr($method->getName(), 3)), $class->identifier, true) &&
|
||||||
|
(($method->getEndLine() - $method->getStartLine()) <= 4)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the code for the __sleep method for a proxy class.
|
* Generates the code for the __sleep method for a proxy class.
|
||||||
*
|
*
|
||||||
|
|
|
@ -78,7 +78,7 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||||
$reference = $this->_em->getReference('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
|
$reference = $this->_em->getReference('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
|
||||||
$this->assertFalse($reference->postLoadCallbackInvoked);
|
$this->assertFalse($reference->postLoadCallbackInvoked);
|
||||||
|
|
||||||
$reference->getId(); // trigger proxy load
|
$reference->getValue(); // trigger proxy load
|
||||||
$this->assertTrue($reference->postLoadCallbackInvoked);
|
$this->assertTrue($reference->postLoadCallbackInvoked);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,6 +210,10 @@ class LifecycleCallbackTestEntity
|
||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getValue() {
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
/** @PrePersist */
|
/** @PrePersist */
|
||||||
public function doStuffOnPrePersist() {
|
public function doStuffOnPrePersist() {
|
||||||
$this->prePersistCallbackInvoked = true;
|
$this->prePersistCallbackInvoked = true;
|
||||||
|
@ -274,4 +278,4 @@ class LifecycleListenerPreUpdate
|
||||||
{
|
{
|
||||||
$eventArgs->setNewValue('name', 'Bob');
|
$eventArgs->setNewValue('name', 'Bob');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,4 +147,28 @@ class ReferenceProxyTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||||
|
|
||||||
$this->assertTrue($entity->wakeUp, "Loading the proxy should call __wakeup().");
|
$this->assertTrue($entity->wakeUp, "Loading the proxy should call __wakeup().");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDoNotInitializeProxyOnGettingTheIdentifier()
|
||||||
|
{
|
||||||
|
$id = $this->createProduct();
|
||||||
|
|
||||||
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
||||||
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
||||||
|
|
||||||
|
$this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
|
||||||
|
$this->assertEquals($id, $entity->getId());
|
||||||
|
$this->assertFalse($entity->__isInitialized__, "Getting the identifier doesn't initialize the proxy.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInitializeProxyOnGettingSomethingOtherThanTheIdentifier()
|
||||||
|
{
|
||||||
|
$id = $this->createProduct();
|
||||||
|
|
||||||
|
/* @var $entity Doctrine\Tests\Models\ECommerce\ECommerceProduct */
|
||||||
|
$entity = $this->_em->getReference('Doctrine\Tests\Models\ECommerce\ECommerceProduct' , $id);
|
||||||
|
|
||||||
|
$this->assertFalse($entity->__isInitialized__, "Pre-Condition: Object is unitialized proxy.");
|
||||||
|
$this->assertEquals('Doctrine Cookbook', $entity->getName());
|
||||||
|
$this->assertTrue($entity->__isInitialized__, "Getting something other than the identifier initializes the proxy.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,8 @@ class DDC1238Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||||
$this->_em->flush();
|
$this->_em->flush();
|
||||||
$this->_em->clear();
|
$this->_em->clear();
|
||||||
|
|
||||||
|
// force proxy load, getId() doesn't work anymore
|
||||||
|
$user->getName();
|
||||||
$userId = $user->getId();
|
$userId = $user->getId();
|
||||||
$this->_em->clear();
|
$this->_em->clear();
|
||||||
|
|
||||||
|
@ -60,6 +62,8 @@ class DDC1238Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||||
|
|
||||||
$user2 = $this->_em->getReference(__NAMESPACE__ . '\\DDC1238User', $userId);
|
$user2 = $this->_em->getReference(__NAMESPACE__ . '\\DDC1238User', $userId);
|
||||||
|
|
||||||
|
// force proxy load, getId() doesn't work anymore
|
||||||
|
$user->getName();
|
||||||
$this->assertNull($user->getId(), "Now this is null, we already have a user instance of that type");
|
$this->assertNull($user->getId(), "Now this is null, we already have a user instance of that type");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,8 @@ class DDC381Test extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||||
|
|
||||||
$entity = $this->_em->getReference('Doctrine\Tests\ORM\Functional\Ticket\DDC381Entity', $persistedId);
|
$entity = $this->_em->getReference('Doctrine\Tests\ORM\Functional\Ticket\DDC381Entity', $persistedId);
|
||||||
|
|
||||||
// explicitly load proxy
|
// explicitly load proxy (getId() does not trigger reload of proxy)
|
||||||
$id = $entity->getId();
|
$id = $entity->getOtherMethod();
|
||||||
|
|
||||||
$data = serialize($entity);
|
$data = serialize($entity);
|
||||||
$entity = unserialize($data);
|
$entity = unserialize($data);
|
||||||
|
@ -55,4 +55,9 @@ class DDC381Entity
|
||||||
{
|
{
|
||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getOtherMethod()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -73,12 +73,13 @@ class ProxyClassGeneratorTest extends \Doctrine\Tests\OrmTestCase
|
||||||
$persister = $this->_getMockPersister();
|
$persister = $this->_getMockPersister();
|
||||||
$this->_uowMock->setEntityPersister('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $persister);
|
$this->_uowMock->setEntityPersister('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $persister);
|
||||||
$proxy = $this->_proxyFactory->getProxy('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $identifier);
|
$proxy = $this->_proxyFactory->getProxy('Doctrine\Tests\Models\ECommerce\ECommerceFeature', $identifier);
|
||||||
|
|
||||||
$persister->expects($this->atLeastOnce())
|
$persister->expects($this->atLeastOnce())
|
||||||
->method('load')
|
->method('load')
|
||||||
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass))
|
->with($this->equalTo($identifier), $this->isInstanceOf($proxyClass))
|
||||||
->will($this->returnValue(new \stdClass())); // fake return of entity instance
|
->will($this->returnValue(new \stdClass())); // fake return of entity instance
|
||||||
$proxy->getId();
|
|
||||||
$proxy->getDescription();
|
$proxy->getDescription();
|
||||||
|
$proxy->getProduct();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testReferenceProxyRespectsMethodsParametersTypeHinting()
|
public function testReferenceProxyRespectsMethodsParametersTypeHinting()
|
||||||
|
@ -179,4 +180,4 @@ class SleepClass
|
||||||
{
|
{
|
||||||
return array('id');
|
return array('id');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue