summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2018-11-13 11:32:26 (GMT)
committerNathaniel Catchpole2018-11-13 11:32:38 (GMT)
commite4b3ec34c1210aadba5e1938c3b427cf4f03d96a (patch)
treefff803d567c161384828a87493fe2e0466926091
parent46ad14aa7c1627a1d990b8d5a552113a207dafd8 (diff)
Issue #2342699 by Beakerboy, david_garcia, alexpott, madhavvyas, Nitesh Sethia, mradcliffe, plach, rakshith.thotada, ruloweb, deepakaryan1988, Berdir, rbrenton: SqlContentEntityStorage tries to update identity/serial values by default
(cherry picked from commit c8f810edb862cf9e7523791f6ff6ea17942b133e)
-rw-r--r--core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php25
-rw-r--r--core/lib/Drupal/Core/Path/AliasStorage.php2
-rw-r--r--core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php30
3 files changed, 48 insertions, 9 deletions
diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
index 3fdf81b..48545d2 100644
--- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
+++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
@@ -821,10 +821,13 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
if ($update) {
$default_revision = $entity->isDefaultRevision();
if ($default_revision) {
+ // Remove the ID from the record to enable updates on SQL variants
+ // that prevent updating serial columns, for example, mssql.
+ unset($record->{$this->idKey});
$this->database
->update($this->baseTable)
->fields((array) $record)
- ->condition($this->idKey, $record->{$this->idKey})
+ ->condition($this->idKey, $entity->get($this->idKey)->value)
->execute();
}
if ($this->revisionTable) {
@@ -833,11 +836,15 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
}
else {
$record = $this->mapToStorageRecord($entity->getUntranslated(), $this->revisionTable);
+ // Remove the revision ID from the record to enable updates on SQL
+ // variants that prevent updating serial columns, for example,
+ // mssql.
+ unset($record->{$this->revisionKey});
$entity->preSaveRevision($this, $record);
$this->database
->update($this->revisionTable)
->fields((array) $record)
- ->condition($this->revisionKey, $record->{$this->revisionKey})
+ ->condition($this->revisionKey, $entity->getRevisionId())
->execute();
}
}
@@ -1064,19 +1071,21 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt
->condition($this->idKey, $record->{$this->idKey})
->execute();
}
+ // Make sure to update the new revision key for the entity.
+ $entity->{$this->revisionKey}->value = $record->{$this->revisionKey};
}
else {
+ // Remove the revision ID from the record to enable updates on SQL
+ // variants that prevent updating serial columns, for example,
+ // mssql.
+ unset($record->{$this->revisionKey});
$this->database
->update($this->revisionTable)
->fields((array) $record)
- ->condition($this->revisionKey, $record->{$this->revisionKey})
+ ->condition($this->revisionKey, $entity->getRevisionId())
->execute();
}
-
- // Make sure to update the new revision key for the entity.
- $entity->{$this->revisionKey}->value = $record->{$this->revisionKey};
-
- return $record->{$this->revisionKey};
+ return $entity->getRevisionId();
}
/**
diff --git a/core/lib/Drupal/Core/Path/AliasStorage.php b/core/lib/Drupal/Core/Path/AliasStorage.php
index a02bc35..b689848 100644
--- a/core/lib/Drupal/Core/Path/AliasStorage.php
+++ b/core/lib/Drupal/Core/Path/AliasStorage.php
@@ -106,11 +106,11 @@ class AliasStorage implements AliasStorageInterface {
$this->catchException($e);
$original = FALSE;
}
- $fields['pid'] = $pid;
$query = $this->connection->update(static::TABLE)
->fields($fields)
->condition('pid', $pid);
$pid = $query->execute();
+ $fields['pid'] = $pid;
$fields['original'] = $original;
$operation = 'update';
}
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
index 3fd17d8..1695d93 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
@@ -3,6 +3,7 @@
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface;
@@ -919,4 +920,33 @@ class EntityFieldTest extends EntityKernelTestBase {
$this->assertEqual($entity->field_test_text->processed, $target, format_string('%entity_type: Text is processed with the default filter.', ['%entity_type' => $entity_type]));
}
+ /**
+ * Tests explicit entity ID assignment.
+ */
+ public function testEntityIdAssignment() {
+ $entity_type = 'entity_test';
+ /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
+ $storage = $this->container->get('entity_type.manager')->getStorage($entity_type);
+
+ // Check that an ID can be explicitly assigned on creation.
+ /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
+ $entity = $this->createTestEntity($entity_type);
+ $entity_id = 3;
+ $entity->set('id', $entity_id);
+ $this->assertSame($entity_id, $entity->id());
+ $storage->save($entity);
+ $entity = $storage->loadUnchanged($entity->id());
+ $this->assertTrue($entity);
+
+ // Check that an explicitly-assigned ID is preserved on update.
+ $storage->save($entity);
+ $entity = $storage->loadUnchanged($entity->id());
+ $this->assertTrue($entity);
+
+ // Check that an ID cannot be explicitly assigned on update.
+ $this->setExpectedException(EntityStorageException::class);
+ $entity->set('id', $entity_id + 1);
+ $storage->save($entity);
+ }
+
}