Commit e4b3ec34 authored by catch's avatar catch

Issue #2342699 by Beakerboy, david_garcia, alexpott, madhavvyas, Nitesh...

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 c8f810ed)
parent 46ad14aa
......@@ -821,10 +821,13 @@ protected function doSaveFieldItems(ContentEntityInterface $entity, array $names
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 @@ protected function doSaveFieldItems(ContentEntityInterface $entity, array $names
}
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 @@ protected function saveRevision(ContentEntityInterface $entity) {
->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();
}
/**
......
......@@ -106,11 +106,11 @@ public function save($source, $alias, $langcode = LanguageInterface::LANGCODE_NO
$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';
}
......
......@@ -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 @@ protected function doTestComputedProperties($entity_type) {
$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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment