Issue #2332577 by Berdir: Remove EntityDatabaseStorage.
parent
e4750a75ad
commit
055aac105d
|
@ -1,212 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Contains \Drupal\Core\Entity\EntityDatabaseStorage.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Drupal\Core\Entity;
|
|
||||||
|
|
||||||
use Drupal\Component\Uuid\UuidInterface;
|
|
||||||
use Drupal\Core\Database\Connection;
|
|
||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines a base entity storage class.
|
|
||||||
*
|
|
||||||
* This class only supports bare, non-content entities.
|
|
||||||
*/
|
|
||||||
class EntityDatabaseStorage extends EntityStorageBase {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The UUID service.
|
|
||||||
*
|
|
||||||
* @var \Drupal\Component\Uuid\UuidInterface
|
|
||||||
*/
|
|
||||||
protected $uuidService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether this entity type should use the static cache.
|
|
||||||
*
|
|
||||||
* @var boolean
|
|
||||||
*/
|
|
||||||
protected $cache;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Active database connection.
|
|
||||||
*
|
|
||||||
* @var \Drupal\Core\Database\Connection
|
|
||||||
*/
|
|
||||||
protected $database;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
|
|
||||||
return new static(
|
|
||||||
$entity_type,
|
|
||||||
$container->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';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -7,3 +7,4 @@ core: 8.x
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
- field_ui
|
- field_ui
|
||||||
|
- entity_test
|
||||||
|
|
|
@ -7,27 +7,21 @@
|
||||||
|
|
||||||
namespace Drupal\field_ui_test\Entity;
|
namespace Drupal\field_ui_test\Entity;
|
||||||
|
|
||||||
use Drupal\Core\Entity\Entity;
|
use Drupal\entity_test\Entity\EntityTest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the test Field UI class.
|
* Defines the test Field UI class.
|
||||||
*
|
*
|
||||||
* @EntityType(
|
* @ContentEntityType(
|
||||||
* id = "field_ui_test_no_bundle",
|
* id = "field_ui_test_no_bundle",
|
||||||
* label = @Translation("Test Field UI entity, no bundle"),
|
* label = @Translation("Test Field UI entity, no bundle"),
|
||||||
* handlers = {
|
* entity_keys = {
|
||||||
* "storage" = "Drupal\Core\Entity\EntityDatabaseStorage"
|
* "id" = "id",
|
||||||
|
* "uuid" = "uuid",
|
||||||
* },
|
* },
|
||||||
* fieldable = TRUE
|
* fieldable = TRUE
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
class FieldUITestNoBundle extends Entity {
|
class FieldUITestNoBundle extends EntityTest {
|
||||||
|
|
||||||
/**
|
|
||||||
* The entity ID.
|
|
||||||
*
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
public $id;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Definition of Drupal\system\Tests\Entity\EntityApiInfoTest.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Drupal\system\Tests\Entity;
|
|
||||||
|
|
||||||
use Drupal\simpletest\WebTestBase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Makes sure entity info is accurately cached.
|
|
||||||
*
|
|
||||||
* @group Entity
|
|
||||||
*/
|
|
||||||
class EntityApiInfoTest extends WebTestBase {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ensures entity info cache is updated after changes.
|
|
||||||
*/
|
|
||||||
function testEntityInfoChanges() {
|
|
||||||
\Drupal::moduleHandler()->install(array('entity_cache_test'));
|
|
||||||
$entity_types = \Drupal::entityManager()->getDefinitions();
|
|
||||||
$this->assertTrue(isset($entity_types['entity_cache_test']), 'Test entity type found.');
|
|
||||||
|
|
||||||
// Change the label of the test entity type and make sure changes appear
|
|
||||||
// after flushing caches.
|
|
||||||
\Drupal::state()->set('entity_cache_test.label', 'New label.');
|
|
||||||
$entity_type = \Drupal::entityManager()->getDefinition('entity_cache_test');
|
|
||||||
$this->assertEqual($entity_type->getLabel(), 'Entity Cache Test', 'Original label appears in cached entity info.');
|
|
||||||
$this->resetAll();
|
|
||||||
$entity_type = \Drupal::entityManager()->getDefinition('entity_cache_test');
|
|
||||||
$this->assertEqual($entity_type->getLabel(), 'New label.', 'New label appears in entity info.');
|
|
||||||
|
|
||||||
// Uninstall the providing module and make sure the entity type is gone.
|
|
||||||
$this->container->get('module_handler')->uninstall(array('entity_cache_test', 'entity_cache_test_dependency'));
|
|
||||||
$entity_types = \Drupal::entityManager()->getDefinitions();
|
|
||||||
$this->assertFalse(isset($entity_types['entity_cache_test']), 'Entity type of the providing module is gone.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests entity info cache after enabling a module with a dependency on an entity providing module.
|
|
||||||
*
|
|
||||||
* @see entity_cache_test_modules_enabled()
|
|
||||||
*/
|
|
||||||
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\EntityDatabaseStorage', 'Entity handler class info is correct.');
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
name: 'Entity cache test'
|
|
||||||
type: module
|
|
||||||
description: 'Support module for testing entity cache.'
|
|
||||||
package: Testing
|
|
||||||
version: VERSION
|
|
||||||
core: 8.x
|
|
||||||
dependencies:
|
|
||||||
- entity_cache_test_dependency
|
|
|
@ -1,24 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Helper module for entity cache tests.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_modules_installed().
|
|
||||||
*
|
|
||||||
* This hook is called during \Drupal\Core\Extension\ModuleHandler::install()
|
|
||||||
* and since this hook implementation is invoked, we have to expect that this
|
|
||||||
* module and dependent modules have been properly installed already. So we
|
|
||||||
* expect to be able to retrieve the entity information that has been registered
|
|
||||||
* by the required dependency module.
|
|
||||||
*
|
|
||||||
* @see EntityApiInfoTest::testEntityInfoCacheModulesEnabled()
|
|
||||||
*/
|
|
||||||
function entity_cache_test_modules_installed($modules_enabled) {
|
|
||||||
$info = \Drupal::entityManager()->getDefinition('entity_cache_test');
|
|
||||||
// Store the information in a system variable to analyze it later in the
|
|
||||||
// test case.
|
|
||||||
\Drupal::state()->set('entity_cache_test', $info);
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
name: 'Entity cache test dependency'
|
|
||||||
type: module
|
|
||||||
description: 'Support dependency module for testing entity cache.'
|
|
||||||
package: Testing
|
|
||||||
version: VERSION
|
|
||||||
core: 8.x
|
|
|
@ -1,14 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Helper module for entity cache tests.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_entity_type_alter().
|
|
||||||
*/
|
|
||||||
function entity_cache_test_dependency_entity_type_alter(array &$entity_types) {
|
|
||||||
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
|
|
||||||
$entity_types['entity_cache_test']->set('label', \Drupal::state()->get('entity_cache_test.label') ?: 'Entity Cache Test');
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Contains Drupal\entity_cache_test_dependency\Entity\EntityCacheTest.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Drupal\entity_cache_test_dependency\Entity;
|
|
||||||
|
|
||||||
use Drupal\Core\Entity\Entity;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines the EntityCacheTest class.
|
|
||||||
*
|
|
||||||
* @EntityType(
|
|
||||||
* id = "entity_cache_test",
|
|
||||||
* label = @Translation("Entity cache test"),
|
|
||||||
* handlers = {
|
|
||||||
* "storage" = "Drupal\Core\Entity\EntityDatabaseStorage",
|
|
||||||
* }
|
|
||||||
* )
|
|
||||||
*/
|
|
||||||
class EntityCacheTest extends Entity {
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue