Skip to content

Commit

Permalink
Merge pull request doctrine#664 from doctrine/proxy_for_parent_nodena…
Browse files Browse the repository at this point in the history
…me_classes

Proxy for parent nodename classes
  • Loading branch information
dbu committed Oct 5, 2015
2 parents 7cd7c32 + bfd45d6 commit ccb4771
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/Doctrine/ODM/PHPCR/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions tests/Doctrine/Tests/ODM/PHPCR/Functional/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit ccb4771

Please sign in to comment.