diff --git a/lib/Doctrine/ODM/PHPCR/Proxy/ProxyFactory.php b/lib/Doctrine/ODM/PHPCR/Proxy/ProxyFactory.php index cb880eb7d..cc9147a98 100644 --- a/lib/Doctrine/ODM/PHPCR/Proxy/ProxyFactory.php +++ b/lib/Doctrine/ODM/PHPCR/Proxy/ProxyFactory.php @@ -93,12 +93,20 @@ protected function createProxyDefinition($className) { $classMetadata = $this->documentManager->getClassMetadata($className); + if ($classMetadata->identifier) { + $identifierFields = array($classMetadata->identifier); + $reflectionId = $classMetadata->reflFields[$classMetadata->identifier]; + } else { + $identifierFields = array(); + $reflectionId = null; + } + return new ProxyDefinition( ClassUtils::generateProxyClassName($classMetadata->getName(), $this->proxyNamespace), - array($classMetadata->identifier), + $identifierFields, $classMetadata->reflFields, $this->createInitializer($classMetadata), - $this->createCloner($classMetadata, $classMetadata->reflFields[$classMetadata->identifier]) + $this->createCloner($classMetadata, $reflectionId) ); } @@ -168,7 +176,7 @@ private function createInitializer(ClassMetadata $classMetadata) * * @throws \Doctrine\Common\Proxy\Exception\UnexpectedValueException */ - private function createCloner(ClassMetadata $classMetadata, ReflectionProperty $reflectionId) + private function createCloner(ClassMetadata $classMetadata, ReflectionProperty $reflectionId = null) { $className = $classMetadata->getName(); $documentManager = $this->documentManager; @@ -181,6 +189,10 @@ private function createCloner(ClassMetadata $classMetadata, ReflectionProperty $ $cloned->__setInitialized(true); $cloned->__setInitializer(null); + if (!$reflectionId) { + return; + } + $original = $documentManager->find($className, $reflectionId->getValue($cloned)); if (null === $original) { diff --git a/tests/Doctrine/Tests/ODM/PHPCR/Functional/ProxyTest.php b/tests/Doctrine/Tests/ODM/PHPCR/Functional/ProxyTest.php index ca1ce8cf1..214e955b3 100644 --- a/tests/Doctrine/Tests/ODM/PHPCR/Functional/ProxyTest.php +++ b/tests/Doctrine/Tests/ODM/PHPCR/Functional/ProxyTest.php @@ -77,4 +77,50 @@ public function testProxyImplicit() $this->assertSame($assistant, $user->child); } + + public function testChildWithoutId() + { + $node = $this->resetFunctionalNode($this->dm); + $parentId = $node->getPath().'/parent'; + + $parent = new ParentDoc(); + $parent->id = $parentId; + + $doc = new DocWithoutId(); + $doc->parent = $parent; + $doc->nodename = 'foo'; + $this->dm->persist($doc); + + $this->dm->flush(); + $this->dm->clear(); + + $parent = $this->dm->find(null, $parentId); + $doc = $parent->children->current(); + $this->assertInstanceOf('Doctrine\Common\Proxy\Proxy', $doc); + $this->assertInstanceOf('Doctrine\Tests\ODM\PHPCR\Functional\DocWithoutId', $doc); + $this->assertEquals('foo', $doc->nodename); + $this->assertInstanceOf('Doctrine\Tests\ODM\PHPCR\Functional\ParentDoc', $doc->parent); + } +} + +/** + * @PHPCRODM\Document() + */ +class ParentDoc +{ + /** @PHPCRODM\Id */ + public $id; + /** @PHPCRODM\Children(cascade="persist") */ + public $children; +} + +/** + * @PHPCRODM\Document() + */ +class DocWithoutId +{ + /** @PHPCRODM\ParentDocument */ + public $parent; + /** @PHPCRODM\Nodename */ + public $nodename; }