diff --git a/core/lib/Drupal/Core/Entity/EntityDatabaseStorage.php b/core/lib/Drupal/Core/Entity/EntityDatabaseStorage.php new file mode 100644 index 0000000..b55736d --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityDatabaseStorage.php @@ -0,0 +1,212 @@ +get('database'), + $container->get('uuid') + ); + } + + /** + * Constructs a EntityDatabaseStorage object. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type definition. + * @param \Drupal\Core\Database\Connection $database + * The database connection to be used. + * @param \Drupal\Component\Uuid\UuidInterface $uuid_service + * The UUID service. + */ + public function __construct(EntityTypeInterface $entity_type, Connection $database, UuidInterface $uuid_service) { + parent::__construct($entity_type); + + $this->database = $database; + $this->uuidService = $uuid_service; + + // Check if the entity type supports UUIDs. + $this->uuidKey = $this->entityType->getKey('uuid'); + } + + /** + * {@inheritdoc} + */ + protected function doLoadMultiple(array $ids = NULL) { + // Build and execute the query. + $records = $this + ->buildQuery($ids) + ->execute() + ->fetchAllAssoc($this->idKey, \PDO::FETCH_ASSOC); + + return $this->mapFromStorageRecords($records); + } + + /** + * {@inheritdoc} + */ + public function loadRevision($revision_id) { + throw new \Exception('Database storage does not support revisions.'); + } + + /** + * {@inheritdoc} + */ + public function deleteRevision($revision_id) { + throw new \Exception('Database storage does not support revisions.'); + } + + /** + * Builds the query to load the entity. + * + * @param array|null $ids + * An array of entity IDs, or NULL to load all entities. + * + * @return \Drupal\Core\Database\Query\Select + * A SelectQuery object for loading the entity. + */ + protected function buildQuery($ids) { + $query = $this->database->select($this->entityType->getBaseTable(), 'base'); + + $query->addTag($this->entityTypeId . '_load_multiple'); + + // Add fields from the {entity} table. + $entity_fields = drupal_schema_fields_sql($this->entityType->getBaseTable()); + $query->fields('base', $entity_fields); + + if ($ids) { + $query->condition("base.{$this->idKey}", $ids, 'IN'); + } + + return $query; + } + + /** + * {@inheritdoc} + */ + public function delete(array $entities) { + if (!$entities) { + // If no IDs or invalid IDs were passed, do nothing. + return; + } + $transaction = $this->database->startTransaction(); + + try { + parent::delete($entities); + + // Ignore replica server temporarily. + db_ignore_replica(); + } + catch (\Exception $e) { + $transaction->rollback(); + watchdog_exception($this->entityTypeId, $e); + throw new EntityStorageException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * {@inheritdoc} + */ + protected function doDelete($entities) { + $ids = array_keys($entities); + + $this->database->delete($this->entityType->getBaseTable()) + ->condition($this->idKey, $ids, 'IN') + ->execute(); + + // Reset the cache as soon as the changes have been applied. + $this->resetCache($ids); + } + + /** + * {@inheritdoc} + */ + public function save(EntityInterface $entity) { + $transaction = $this->database->startTransaction(); + try { + $return = parent::save($entity); + + // Ignore replica server temporarily. + db_ignore_replica(); + return $return; + } + catch (\Exception $e) { + $transaction->rollback(); + watchdog_exception($this->entityTypeId, $e); + throw new EntityStorageException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * {@inheritdoc} + */ + protected function doSave($id, EntityInterface $entity) { + if (!$entity->isNew()) { + $return = drupal_write_record($this->entityType->getBaseTable(), $entity, $this->idKey); + $this->resetCache(array($entity->id())); + } + else { + $return = drupal_write_record($this->entityType->getBaseTable(), $entity); + // Reset general caches, but keep caches specific to certain entities. + $this->resetCache(array()); + } + + return $return; + } + + /** + * {@inheritdoc} + */ + protected function has($id, EntityInterface $entity) { + return !$entity->isNew(); + } + + /** + * {@inheritdoc} + */ + public function getQueryServiceName() { + return 'entity.query.sql'; + } + +} diff --git a/core/modules/field_ui/tests/modules/field_ui_test/src/Entity/FieldUITestNoBundle.php b/core/modules/field_ui/tests/modules/field_ui_test/src/Entity/FieldUITestNoBundle.php index 7aeceb6..be18202 100644 --- a/core/modules/field_ui/tests/modules/field_ui_test/src/Entity/FieldUITestNoBundle.php +++ b/core/modules/field_ui/tests/modules/field_ui_test/src/Entity/FieldUITestNoBundle.php @@ -16,7 +16,7 @@ * id = "field_ui_test_no_bundle", * label = @Translation("Test Field UI entity, no bundle"), * handlers = { - * "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage" + * "storage" = "Drupal\Core\Entity\EntityDatabaseStorage" * }, * fieldable = TRUE * ) diff --git a/core/modules/system/src/Tests/Entity/EntityApiInfoTest.php b/core/modules/system/src/Tests/Entity/EntityApiInfoTest.php index dab64bd..8b48f95 100644 --- a/core/modules/system/src/Tests/Entity/EntityApiInfoTest.php +++ b/core/modules/system/src/Tests/Entity/EntityApiInfoTest.php @@ -48,6 +48,6 @@ function testEntityInfoCacheModulesEnabled() { \Drupal::moduleHandler()->install(array('entity_cache_test')); $entity_type = \Drupal::state()->get('entity_cache_test'); $this->assertEqual($entity_type->getLabel(), 'Entity Cache Test', 'Entity info label is correct.'); - $this->assertEqual($entity_type->getStorageClass(), 'Drupal\Core\Entity\Sql\SqlContentEntityStorage', 'Entity handler class info is correct.'); + $this->assertEqual($entity_type->getStorageClass(), 'Drupal\Core\Entity\EntityDatabaseStorage', 'Entity handler class info is correct.'); } } diff --git a/core/modules/system/tests/modules/entity_cache_test_dependency/src/Entity/EntityCacheTest.php b/core/modules/system/tests/modules/entity_cache_test_dependency/src/Entity/EntityCacheTest.php index 38a0da8..4c9c243 100644 --- a/core/modules/system/tests/modules/entity_cache_test_dependency/src/Entity/EntityCacheTest.php +++ b/core/modules/system/tests/modules/entity_cache_test_dependency/src/Entity/EntityCacheTest.php @@ -16,7 +16,7 @@ * id = "entity_cache_test", * label = @Translation("Entity cache test"), * handlers = { - * "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage", + * "storage" = "Drupal\Core\Entity\EntityDatabaseStorage", * } * ) */