Permalink
Browse files

fix handling of repository strategy for children collection

  • Loading branch information...
1 parent b6733b9 commit e19624b2cfffd3af0737451086ef83b16f939053 @lsmith77 lsmith77 committed Feb 28, 2013
Showing with 47 additions and 46 deletions.
  1. +4 −4 lib/Doctrine/ODM/PHPCR/UnitOfWork.php
  2. +43 −42 tests/Doctrine/Tests/ODM/PHPCR/Functional/ChildrenTest.php
@@ -903,13 +903,13 @@ private function getDocumentActualData(ClassMetadata $class, $document)
return $actualData;
}
- private function getChildNodename($id, $nodename, $child)
+ private function getChildNodename($id, $nodename, $child, $parent)
{
$childClass = $this->dm->getClassMetadata(get_class($child));
if ($childClass->nodename && $childClass->reflFields[$childClass->nodename]->getValue($child)) {
$nodename = $childClass->reflFields[$childClass->nodename]->getValue($child);
} else {
- $childId = $childClass->getIdentifierValue($child);
+ $childId = $childClass->getIdentifierValue($child) ?: $this->getIdGenerator($childClass->idGenerator)->generate($child, $childClass, $this->dm, $parent);
if ('' !== $childId) {
if ($childId !== $id.'/'.basename($childId)) {
throw PHPCRException::cannotMoveByAssignment(self::objToStr($child, $this->dm));
@@ -955,7 +955,7 @@ public function computeChangeSet(ClassMetadata $class, $document)
$mapping = $class->mappings[$fieldName];
if ($actualData[$fieldName]) {
foreach ($actualData[$fieldName] as $nodename => $child) {
- $nodename = $this->getChildNodename($id, $nodename, $child);
+ $nodename = $this->getChildNodename($id, $nodename, $child, $document);
$actualData[$fieldName][$nodename] = $this->computeChildChanges($mapping, $child, $id, $nodename, $document);
$childNames[] = $nodename;
}
@@ -1070,7 +1070,7 @@ public function computeChangeSet(ClassMetadata $class, $document)
$childNames = array();
if ($actualData[$fieldName]) {
foreach ($actualData[$fieldName] as $nodename => $child) {
- $nodename = $this->getChildNodename($id, $nodename, $child);
+ $nodename = $this->getChildNodename($id, $nodename, $child, $document);
$actualData[$fieldName][$nodename] = $this->computeChildChanges($mapping, $child, $id, $nodename, $document);
$childNames[] = $nodename;
}
@@ -38,22 +38,22 @@ public function setUp()
$this->dm->persist($parent);
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-a';
+ $child->id = '/functional/parent/Child A';
$child->name = 'Child A';
$this->dm->persist($child);
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-b';
+ $child->id = '/functional/parent/Child B';
$child->name = 'Child B';
$this->dm->persist($child);
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-c';
+ $child->id = '/functional/parent/Child C';
$child->name = 'Child C';
$this->dm->persist($child);
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-d';
+ $child->id = '/functional/parent/Child D';
$child->name = 'Child D';
$this->dm->persist($child);
@@ -67,29 +67,29 @@ public function testChildrenCollection()
$col = $this->dm->getChildren($parent);
$this->assertCount(4, $col);
- $childA = $col['child-a'];
+ $childA = $col['Child A'];
$this->assertEquals('Child A', $childA->name);
- $this->assertEquals('child-a', $col->key());
+ $this->assertEquals('Child A', $col->key());
- $col = $this->dm->getChildren($parent, 'child*');
+ $col = $this->dm->getChildren($parent, 'Child*');
$this->assertCount(4, $col);
- $col = $this->dm->getChildren($parent, '*a');
+ $col = $this->dm->getChildren($parent, '*A');
$this->assertCount(1, $col);
$this->assertTrue($childA === $col->first());
$this->dm->clear();
- $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/child-d');
+ $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/Child D');
$parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent');
$col = $this->dm->getChildren($parent);
- $this->assertEquals('child-a', $col->key());
+ $this->assertEquals('Child A', $col->key());
$this->dm->clear();
- $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/child-d');
+ $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/Child D');
$parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent');
- $this->assertEquals('child-a', $parent->allChildren->key());
+ $this->assertEquals('Child A', $parent->allChildren->key());
}
public function testNoChildrenInitOnFlush()
@@ -142,24 +142,24 @@ public function testCreateChildren()
{
$children = array();
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-a/child-create-1';
+ $child->id = '/functional/parent/Child A/Child Create-1';
$child->name = 'Child A';
$children[] = $child;
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-a/child-create-2';
+ $child->id = '/functional/parent/Child A/Child Create-2';
$child->name = 'Child B';
$children[] = $child;
- $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/child-a');
+ $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/Child A');
$this->assertCount(0, $parent->allChildren);
$parent->allChildren = $children;
$this->dm->persist($parent);
$this->dm->flush();
$this->dm->clear();
- $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/child-a');
+ $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/Child A');
$this->assertCount(2, $parent->allChildren);
}
@@ -184,11 +184,11 @@ public function testModifyChildren()
$child = $parent->allChildren->first();
$child->name = 'New name';
- $parent->allChildren->remove('child-b');
- $parent->allChildren->remove('child-c');
+ $parent->allChildren->remove('Child B');
+ $parent->allChildren->remove('Child C');
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-e';
+ $child->id = '/functional/parent/Child E';
$child->name = 'Child E';
$parent->allChildren->add($child);
@@ -210,13 +210,13 @@ public function testModifyChildren()
$this->assertCount(0, $parent->allChildren);
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-f';
+ $child->id = '/functional/parent/Child F';
$child->name = 'Child F';
$parent->allChildren->add($child);
$child = new ChildrenTestObj();
- $child->id = '/functional/parent/child-g';
+ $child->id = '/functional/parent/Child G';
$child->name = 'Child G';
$parent->allChildren->add($child);
@@ -231,19 +231,20 @@ public function testReplaceChildren()
$parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent');
$this->assertCount(4, $parent->allChildren);
- $parent->allChildren->remove('child-a');
+ $parent->allChildren->remove('Child A');
$newChild = new ChildrenTestObj();
- $newChild->name = 'child-a';
+ $newChild->name = 'Child A';
$parent->allChildren->add($newChild);
$this->dm->flush();
$this->dm->clear();
$parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent');
- $this->assertTrue($parent->allChildren->containsKey('child-a'));
- $this->assertEquals('child-a', $parent->allChildren['child-a']->name);
+ $this->assertTrue($parent->allChildren->containsKey('Child A'));
+ $this->assertFalse($parent->allChildren->containsKey('0'));
+ $this->assertEquals('Child A', $parent->allChildren['Child A']->name);
}
/**
@@ -263,7 +264,7 @@ public function testMoveByAssignment()
$this->assertCount(4, $parent->allChildren);
$other = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/other');
- $other->allChildren->add($parent->allChildren['child-a']);
+ $other->allChildren->add($parent->allChildren['Child A']);
$this->dm->flush();
}
@@ -287,8 +288,8 @@ public function testReorderChildren()
$this->assertCount(2, $parent->allChildren);
$data = array(
- 'child-g' => $parent->allChildren->last(),
- 'child-f' => $parent->allChildren->first(),
+ 'Child G' => $parent->allChildren->last(),
+ 'Child F' => $parent->allChildren->first(),
);
$parent->allChildren = new ArrayCollection($data);
@@ -310,11 +311,11 @@ public function testReorderChildren()
$child3->name = 'Child J';
$data = array(
- 'child-i' => $child2,
- 'child-h' => $child1,
- 'child-f' => $parent->allChildren->last(),
- 'child-g' => $parent->allChildren->first(),
- 'child-j' => $child3
+ 'Child I' => $child2,
+ 'Child H' => $child1,
+ 'Child F' => $parent->allChildren->last(),
+ 'Child G' => $parent->allChildren->first(),
+ 'Child J' => $child3
);
$parent->allChildren = new ArrayCollection($data);
@@ -337,11 +338,11 @@ public function testReorderChildren()
$this->dm->clear();
$keys = array(
- 'child-i',
- 'child-h',
- 'child-f',
- 'child-j',
- 'child-g',
+ 'Child I',
+ 'Child H',
+ 'Child F',
+ 'Child J',
+ 'Child G',
);
$parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent');
@@ -369,7 +370,7 @@ public function testReorderChildrenLast()
$childrenCollection->clear();
- $expectedOrder = array('child-a', 'child-d', 'child-c', 'child-b');
+ $expectedOrder = array('Child A', 'Child D', 'Child C', 'Child B');
foreach ($expectedOrder as $name) {
$childrenCollection->set($name, $children[$name]);
@@ -396,14 +397,14 @@ public function testInsertWithCustomIdStrategy()
$child->name = 'ChildB';
$children[] = $child;
- $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/child-a');
+ $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/Child A');
$this->assertCount(0, $parent->allChildren);
$parent->allChildren = $children;
$this->dm->flush();
$this->dm->clear();
- $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/child-a');
+ $parent = $this->dm->find('Doctrine\Tests\ODM\PHPCR\Functional\ChildrenTestObj', '/functional/parent/Child A');
$this->assertCount(2, $parent->allChildren);
}
}
@@ -419,7 +420,7 @@ class ChildrenTestObj
/** @PHPCRODM\String */
public $name;
- /** @PHPCRODM\Children(filter="*a", fetchDepth=1, cascade="persist") */
+ /** @PHPCRODM\Children(filter="*A", fetchDepth=1, cascade="persist") */
public $aChildren;
/**

0 comments on commit e19624b

Please sign in to comment.