Skip to content

Commit

Permalink
Merge pull request doctrine#102 from doctrine/reference_issue
Browse files Browse the repository at this point in the history
Reference issue
  • Loading branch information
lsmith77 committed Feb 6, 2012
2 parents 034cc0f + dcc95fe commit d98ffe6
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Expand Up @@ -1172,13 +1172,7 @@ private function executeInserts($documents)
throw new PHPCRException('Register phpcr:managed node type first. See https://github.com/doctrine/phpcr-odm/wiki/Custom-node-type-phpcr:managed');
}

if ($class->versionable === 'full') {
$node->addMixin('mix:versionable');
} elseif ($class->versionable === 'simple') {
$node->addMixin('mix:simpleVersionable');
}

$this->checkReferenceable($class, $node);
$this->setMixins($class, $node);

foreach ($this->documentChangesets[$oid] as $fieldName => $fieldValue) {
// Ignore translatable fields (they will be persisted by the translation strategy)
Expand Down Expand Up @@ -1279,7 +1273,7 @@ private function executeUpdates($documents, $dispatchEvents = true)
}
$refOid = spl_object_hash($fv);
$refClass = $this->dm->getClassMetadata(get_class($fv));
$this->checkReferenceable($refClass, $this->nodesMap[$refOid]);
$this->setMixins($refClass, $this->nodesMap[$refOid]);
if (!$this->nodesMap[$refOid]->isNodeType('mix:referenceable')) {
throw new PHPCRException(sprintf('Referenced document %s is not referenceable. Use referenceable=true in Document annotation: '.self::objToStr($document), get_class($fv)));
}
Expand All @@ -1294,7 +1288,7 @@ private function executeUpdates($documents, $dispatchEvents = true)
if (isset($fieldValue)) {
$refOid = spl_object_hash($fieldValue);
$refClass = $this->dm->getClassMetadata(get_class($fieldValue));
$this->checkReferenceable($refClass, $this->nodesMap[$refOid]);
$this->setMixins($refClass, $this->nodesMap[$refOid]);
if (!$this->nodesMap[$refOid]->isNodeType('mix:referenceable')) {
throw new PHPCRException(sprintf('Referenced document %s is not referenceable. Use referenceable=true in Document annotation: '.self::objToStr($document), get_class($fieldValue)));
}
Expand Down Expand Up @@ -1906,18 +1900,21 @@ protected function checkFullVersioning(Mapping\ClassMetadata $metadata, NodeInte
$node->addMixin('mix:versionable');
}

protected function checkReferenceable(Mapping\ClassMetadata $metadata, NodeInterface $node)
protected function setMixins(Mapping\ClassMetadata $metadata, NodeInterface $node)
{
if (!$node->isNodeType('mix:referenceable')) {
if ($metadata->versionable === 'full') {
$node->addMixin('mix:versionable');
} elseif ($metadata->referenceable) {
if ($metadata->versionable === 'full') {
$node->addMixin('mix:versionable');
} else {
if ($metadata->versionable === 'simple') {
$node->addMixin('mix:simpleVersionable');
}

if ($metadata->referenceable) {
$node->addMixin('mix:referenceable');
}
}

// we manually set the uuid to allow creating referenced and referencing document without flush in between.
// this check has to be done after any mixin types are set.
if ($node->isNodeType('mix:referenceable') && !$node->hasProperty('jcr:uuid')) {
// TODO do we need to check with the storage backend if the generated id really is unique?
$node->setProperty('jcr:uuid', UUIDHelper::generateUUID());
Expand Down

0 comments on commit d98ffe6

Please sign in to comment.