Permalink
Browse files

Implement logic for idGeneratorStrategy

- if parentdocument and nodename are supplied and no extra id strategy is specified use parentidstrategy
- if parentdocument and nodename and id strategy is specified use specified strategy
- otherwise use specified id strategy or use assigned id strategy as default
  • Loading branch information...
1 parent 32229ab commit fabede63080f3e2c77745a1600e27e9fa48a4c4b @uwej711 committed Mar 8, 2012
@@ -84,7 +84,7 @@ class TranslatableProperty extends Property
public $id = true;
public $type = 'string';
/** @var string */
- public $strategy = 'assigned';
+ public $strategy;
}
/**
* @Annotation
@@ -45,6 +45,11 @@ class ClassMetadata implements ClassMetadataInterface
const MANY_TO_ONE = 4;
const MANY_TO_MANY = 8;
+ /**
+ * means no strategy has been set so far.
+ */
+ const GENERATOR_TYPE_NONE = 0;
+
/**
* means the repository will need to be able to generate the id.
*/
@@ -84,6 +89,13 @@ class ClassMetadata implements ClassMetadataInterface
*/
public $idGenerator = self::GENERATOR_TYPE_ASSIGNED;
+ /**
+ * keep track whether an id strategy was explicitly set
+ *
+ * @var boolean
+ */
+ private $idStrategySet = false;
+
/**
* READ-ONLY: The field name of the document identifier.
*/
@@ -459,10 +471,11 @@ public function mapId(array $mapping)
if (isset($mapping['id']) && $mapping['id'] === true) {
$mapping['type'] = 'string';
$this->setIdentifier($mapping['fieldName']);
- if (null !== $this->parentMapping) {
- $this->setIdGenerator(self::GENERATOR_TYPE_PARENT);
- } else if (isset($mapping['strategy'])) {
+ if (isset($mapping['strategy'])) {
$this->setIdGenerator($mapping['strategy']);
+ $this->idStrategySet = true;
+ } elseif (null !== $this->parentMapping && null !== $this->nodename) {
+ $this->setIdGenerator(self::GENERATOR_TYPE_PARENT);
}
}
@@ -489,13 +502,19 @@ public function mapNodename(array $mapping)
{
$this->validateAndCompleteFieldMapping($mapping, false);
$this->nodename = $mapping['fieldName'];
- }
+ if (null !== $this->parentMapping && !$this->idStrategySet)
+ {
+ $this->setIdGenerator(self::GENERATOR_TYPE_PARENT);
+ }}
public function mapParentDocument(array $mapping)
{
$this->validateAndCompleteFieldMapping($mapping, false);
$this->parentMapping = $mapping['fieldName'];
- $this->setIdGenerator(self::GENERATOR_TYPE_PARENT);
+ if (null !== $this->nodename && !$this->idStrategySet)
+ {
+ $this->setIdGenerator(self::GENERATOR_TYPE_PARENT);
+ }
}
public function mapChild(array $mapping)
@@ -652,7 +671,7 @@ public function getFieldMapping($fieldName)
/**
* Sets the ID generator used to generate IDs for instances of this class.
*
- * @param AbstractIdGenerator $generator
+ * @param string $generator
*/
public function setIdGenerator($generator)
{
@@ -4,6 +4,7 @@
use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface,
Doctrine\ODM\PHPCR\DocumentRepository,
+ Doctrine\ODM\PHPCR\Mapping\ClassMetadata,
Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM,
Doctrine\ODM\PHPCR\Translation\LocaleChooser\LocaleChooser;
@@ -75,6 +76,22 @@ public function testSecoundLevelOverwrite()
$this->assertEquals($tmpDocEn->text, 'english');
}
+ public function testIdStrategy()
+ {
+ $metadata = $this->dm->getClassMetadata('\Doctrine\Tests\ODM\PHPCR\Functional\ParentIdStrategy');
+ $this->assertEquals(ClassMetadata::GENERATOR_TYPE_PARENT, $metadata->idGenerator, 'parentId');
+ $metadata = $this->dm->getClassMetadata('\Doctrine\Tests\ODM\PHPCR\Functional\ParentIdStrategyDifferentOrder');
+ $this->assertEquals(ClassMetadata::GENERATOR_TYPE_PARENT, $metadata->idGenerator, 'parentId2');
+ $metadata = $this->dm->getClassMetadata('\Doctrine\Tests\ODM\PHPCR\Functional\AssignedIdStrategy');
+ $this->assertEquals(ClassMetadata::GENERATOR_TYPE_ASSIGNED, $metadata->idGenerator, 'assigned');
+ $metadata = $this->dm->getClassMetadata('\Doctrine\Tests\ODM\PHPCR\Functional\RepositoryIdStrategy');
+ $this->assertEquals(ClassMetadata::GENERATOR_TYPE_REPOSITORY, $metadata->idGenerator, 'repository');
+ $metadata = $this->dm->getClassMetadata('\Doctrine\Tests\ODM\PHPCR\Functional\AutoAssignedIdStrategy');
+ $this->assertEquals(ClassMetadata::GENERATOR_TYPE_ASSIGNED, $metadata->idGenerator, 'autoassigned');
+ $metadata = $this->dm->getClassMetadata('\Doctrine\Tests\ODM\PHPCR\Functional\StandardCase');
+ $this->assertEquals(ClassMetadata::GENERATOR_TYPE_ASSIGNED, $metadata->idGenerator, 'standardcase');
+ }
+
// TODO comprehensive test for all possible mapped fields in an abstract test, trying to persist and check if properly set
// then dm->clear and check if still properly set.
@@ -136,3 +153,84 @@ class SecondLevelWithDuplicateOverwrite extends ExtendingClass
/** @PHPCRODM\String(translated=true) */
public $text;
}
+
+/**
+ * @PHPCRODM\Document
+ */
+class ParentIdStrategy
+{
+ /** @PHPCRODM\Id */
+ public $id;
+
+ /** @PHPCRODM\Nodename */
+ public $name;
+
+ /** @PHPCRODM\ParentDocument */
+ public $parent;
+}
+
+/**
+ * @PHPCRODM\Document
+ */
+class ParentIdStrategyDifferentOrder
+{
+ /** @PHPCRODM\Nodename */
+ public $name;
+
+ /** @PHPCRODM\ParentDocument */
+ public $parent;
+
+ /** @PHPCRODM\Id */
+ public $id;
+}
+
+/**
+ * @PHPCRODM\Document
+ */
+class AssignedIdStrategy
+{
+ /** @PHPCRODM\Id(strategy="assigned") */
+ public $id;
+
+ /** @PHPCRODM\Nodename */
+ public $name;
+
+ /** @PHPCRODM\ParentDocument */
+ public $parent;
+}
+
+/**
+ * @PHPCRODM\Document
+ */
+class RepositoryIdStrategy
+{
+ /** @PHPCRODM\Nodename */
+ public $name;
+
+ /** @PHPCRODM\ParentDocument */
+ public $parent;
+
+ /** @PHPCRODM\Id(strategy="repository") */
+ public $id;
+}
+
+/**
+* @PHPCRODM\Document
+*/
+class AutoAssignedIdStrategy
+{
+ /** @PHPCRODM\ParentDocument */
+ public $parent;
+
+ /** @PHPCRODM\Id() */
+ public $id;
+}
+
+/**
+ * @PHPCRODM\Document
+ */
+class StandardCase
+{
+ /** @PHPCRODM\Id */
+ public $id;
+}

0 comments on commit fabede6

Please sign in to comment.