Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored reference update #231

Merged
merged 2 commits into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: php

sudo: false

php:
- 5.3
- 5.4
Expand Down
33 changes: 23 additions & 10 deletions src/Jackalope/Transport/DoctrineDBAL/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,27 +798,40 @@ private function syncReferences()
throw new RepositoryException('Unexpected exception while cleaning up after saving', $e->getCode(), $e);
}

$updates = array();
foreach ($toUpdate as $nodeId => $references) {
foreach ($references['properties'] as $name => $data) {
foreach ($data['values'] as $value) {
try {
$params = array(
'source_id' => $nodeId,
'source_property_name' => $name,
'target_id' => $this->pathExists(self::getNodePathForIdentifier($value)),
$targetId = $this->pathExists(self::getNodePathForIdentifier($value));

$key = $targetId . '-' . $nodeId . '-' . $name;
// it is valid to have multiple references to the same node in a multivalue
// but it is not desired to store duplicates in the database
$updates[$key] = array(
'type' => $data['type'],
'data' => array(
'source_id' => $nodeId,
'source_property_name' => $name,
'target_id' => $targetId,
),
);

$this->getConnection()->insert($this->referenceTables[$data['type']], $params);
} catch (ItemNotFoundException $e) {
if (PropertyType::REFERENCE === $data['type']) {
throw new ReferentialIntegrityException(
"Trying to store reference to non-existant node with path '$value' in node '{$references['path']}' property '$name'."
);
throw new ReferentialIntegrityException(sprintf(
'Trying to store reference to non-existant node with path "%s" in node "%s" "%s"',
$value, $references['path'], $name
));
}
}

}
}
}

foreach ($updates as $update) {
$this->conn->insert($this->referenceTables[$update['type']], $update['data']);
}
}

// TODO on RDBMS that support deferred FKs we could skip this step
Expand Down Expand Up @@ -996,7 +1009,7 @@ private function propsToXML($properties, $inlineBinaries = false)
case PropertyType::REFERENCE:
$references[$property->getName()] = array(
'type' => $property->getType(),
'values' => $property->isMultiple() ? array_unique($property->getString()) : array($property->getString()),
'values' => $property->isMultiple() ? $property->getString() : array($property->getString()),
);
case PropertyType::NAME:
case PropertyType::URI:
Expand Down