getTableMapping example

$entity = $storage->load($entity->id());
    $this->assertEquals('swanky', $entity->custom_bundle_field->value, 'Entity was saved correctly');

    $entity->custom_bundle_field->value = 'cozy';
    $entity->save();
    $storage->resetCache();
    $entity = $storage->load($entity->id());
    $this->assertEquals('cozy', $entity->custom_bundle_field->value, 'Entity was updated correctly.');

    $entity->delete();
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $storage->getTableMapping();
    $table = $table_mapping->getDedicatedDataTableName($entity->getFieldDefinition('custom_bundle_field')->getFieldStorageDefinition());
    $result = $this->database->select($table, 'f')
      ->fields('f')
      ->condition('f.entity_id', $entity->id())
      ->execute();
    $this->assertFalse($result->fetchAssoc(), 'Field data has been deleted');

    // Create another entity to test that values are marked as deleted when a     // bundle is deleted.     $entity = $storage->create(['type' => 'custom', 'custom_bundle_field' => 'new']);
    $entity->save();
    
public function testCustomFieldCreateDelete() {
    // Install the module which adds the field.     $this->installModule('entity_schema_test');
    $storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_update');
    $this->assertNotNull($storage_definitions['custom_base_field'], 'Base field definition found.');
    $this->assertNotNull($storage_definitions['custom_bundle_field'], 'Bundle field definition found.');

    // Make sure the field schema can be created.     \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_base_field']);
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definitions['custom_bundle_field']);
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->entityTypeManager->getStorage('entity_test_update')->getTableMapping();
    $base_table = current($table_mapping->getTableNames());
    $base_column = current($table_mapping->getColumnNames('custom_base_field'));
    $this->assertTrue($this->database->schema()->fieldExists($base_table$base_column), 'Table column created');
    $table = $table_mapping->getDedicatedDataTableName($storage_definitions['custom_bundle_field']);
    $this->assertTrue($this->database->schema()->tableExists($table), 'Table created');

    // Make sure the field schema can be deleted.     \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definitions['custom_base_field']);
    \Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionDelete($storage_definitions['custom_bundle_field']);
    $this->assertFalse($this->database->schema()->fieldExists($base_table$base_column), 'Table column dropped');
    $this->assertFalse($this->database->schema()->tableExists($table), 'Table dropped');
  }


  /** * Tests getTableMapping() with an empty entity type. * * @covers ::__construct * @covers ::getTableMapping */
  public function testGetTableMappingEmpty() {
    $this->setUpEntityStorage();

    $mapping = $this->entityStorage->getTableMapping();
    $this->assertSame(['entity_test']$mapping->getTableNames());
    $this->assertSame([]$mapping->getFieldNames('entity_test'));
    $this->assertSame([]$mapping->getExtraColumns('entity_test'));
  }

  /** * Tests getTableMapping() with a simple entity type. * * @param string[] $entity_keys * A map of entity keys to use for the mocked entity type. * * @covers ::__construct * @covers ::getTableMapping * * @dataProvider providerTestGetTableMappingSimple */
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;

/** * Storage handler for menu_link_content entities. */
class MenuLinkContentStorage extends SqlContentEntityStorage implements MenuLinkContentStorageInterface {

  /** * {@inheritdoc} */
  public function getMenuLinkIdsWithPendingRevisions() {
    $table_mapping = $this->getTableMapping();
    $id_field = $table_mapping->getColumnNames($this->entityType->getKey('id'))['value'];
    $revision_field = $table_mapping->getColumnNames($this->entityType->getKey('revision'))['value'];
    $rta_field = $table_mapping->getColumnNames($this->entityType->getKey('revision_translation_affected'))['value'];
    $langcode_field = $table_mapping->getColumnNames($this->entityType->getKey('langcode'))['value'];
    $revision_default_field = $table_mapping->getColumnNames($this->entityType->getRevisionMetadataKey('revision_default'))['value'];

    $query = $this->database->select($this->getRevisionDataTable(), 'mlfr');
    $query->fields('mlfr', [$id_field]);
    $query->addExpression("MAX([mlfr].[$revision_field])", $revision_field);

    $query->join($this->getRevisionTable(), 'mlr', "[mlfr].[$revision_field] = [mlr].[$revision_field] AND [mlr].[$revision_default_field] = 0");

    
    $field = FieldConfig::loadByName($this->entityTypeId, $bundle$field_name);
    $field->delete();

    // The field still exists, deleted.     $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
    $this->assertCount(1, $fields, 'There is one deleted field');
    $field = $fields[$field->uuid()];
    $this->assertEquals($bundle$field->getTargetBundle(), 'The deleted field is for the correct bundle');

    // Check that the actual stored content did not change during delete.     /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $storage->getTableMapping();
    $table = $table_mapping->getDedicatedDataTableName($field_storage);
    $column = $table_mapping->getFieldColumnName($field_storage, 'value');
    $result = Database::getConnection()->select($table, 't')
      ->fields('t')
      ->execute();
    foreach ($result as $row) {
      $this->assertEquals($row->{$column}$this->entities[$row->entity_id]->{$field_name}->value);
    }

    // There are 0 entities of this bundle with non-deleted data.     $found = $storage
      

  protected function installEntitySchema($entity_type_id) {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    \Drupal::service('entity_type.listener')->onEntityTypeCreate($entity_type);

    // For test runs, the most common storage backend is a SQL database. For     // this case, ensure the tables got created.     $storage = $entity_type_manager->getStorage($entity_type_id);
    if ($storage instanceof SqlEntityStorageInterface) {
      $tables = $storage->getTableMapping()->getTableNames();
      $db_schema = $this->container->get('database')->schema();
      foreach ($tables as $table) {
        $this->assertTrue($db_schema->tableExists($table), "The entity type table '$table' for the entity type '$entity_type_id' should exist.");
      }
    }
  }

  /** * Enables modules for this test. * * This method does not install modules fully. Services and hooks for the * module are available, but the install process is not performed. * * To install test modules outside of the testing environment, add * @code * $settings['extension_discovery_scan_tests'] = TRUE; * @endcode * to your settings.php. * * @param string[] $modules * A list of modules to enable. Dependencies are not resolved; i.e., * multiple modules have to be specified individually. The modules are only * added to the active module list and loaded; i.e., their database schema * is not installed. hook_install() is not invoked. A custom module weight * is not applied. * * @throws \LogicException * If any module in $modules is already enabled. * @throws \RuntimeException * If a module is not enabled after enabling it. */
/** * {@inheritdoc} */
  public function clickSort($order) {
    $this->ensureMyTable();

    // This could be derived from the content_moderation_state entity table     // mapping, however this is an internal entity type whose storage should     // remain constant.     $storage = $this->entityTypeManager->getStorage('content_moderation_state');
    $storage_definition = $this->entityFieldManager->getActiveFieldStorageDefinitions('content_moderation_state')['moderation_state'];
    $column_name = $storage->getTableMapping()->getFieldColumnName($storage_definition, 'value');
    $this->aliases[$column_name] = $this->tableAlias . '.' . $column_name;

    $this->query->addOrderBy(NULL, NULL, $order$this->aliases[$column_name]);
  }

}

        $source = self::CLASS_HEADER;
        $className = $table->getName();

        // Check if the passed table is a Shopware attribute table.         if (strpos($table->getName(), '_attributes')) {
            // if the table is an attribute table we have to use the class name of the parent table.             $parentClass = str_replace('_attributes', '', $table->getName());
            $className = $this->getClassNameOfTableName($parentClass);

            // If the passed table is not an attribute table, we have to check if the table is already declared         } elseif (\array_key_exists($table->getName()$this->getTableMapping())) {
            // If this is the case we will use the already declared class name             $className = $this->tableMapping[$table->getName()]['class'];
        }

        $source = str_replace('%className%', $className$source);
        $source = str_replace('%tableName%', $table->getName()$source);

        return $source;
    }

    /** * Helper function to get a class name of the passed table. * This function uses the internal property "tableMapping". * The tableMapping array contains the class names and namespace for * each already declared shopware model/table. * * @param string $tableName * * @return string */

  protected function alterQueryForEntityType(Sql $query, EntityTypeInterface $entity_type) {
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->entityTypeManager->getStorage($entity_type->id())->getTableMapping();
    $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
    $dedicated_field_storage_definitions = array_filter($field_storage_definitionsfunction D$definition) use ($table_mapping) {
      return $table_mapping->requiresDedicatedTableStorage($definition);
    });
    $dedicated_field_data_tables = array_map(function D$definition) use ($table_mapping) {
      return $table_mapping->getDedicatedDataTableName($definition);
    }$dedicated_field_storage_definitions);

    $move_workspace_tables = [];
    $table_queue =& $query->getTableQueue();
    foreach ($table_queue as $alias => &$table_info) {
      
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($entity_type_id);

    // If the entity type is not using core's default entity storage, we can't     // assume the table mapping layout so we have to return only the latest     // tracked revisions.     if (!$storage instanceof SqlContentEntityStorage) {
      return $this->getTrackedEntities($workspace_id$entity_type_id$entity_ids)[$entity_type_id];
    }

    $entity_type = $storage->getEntityType();
    $table_mapping = $storage->getTableMapping();
    $workspace_field = $table_mapping->getColumnNames($entity_type->get('revision_metadata_keys')['workspace'])['target_id'];
    $id_field = $table_mapping->getColumnNames($entity_type->getKey('id'))['value'];
    $revision_id_field = $table_mapping->getColumnNames($entity_type->getKey('revision'))['value'];

    $workspace_tree = $this->workspaceRepository->loadTree();
    if (isset($workspace_tree[$workspace_id])) {
      $workspace_candidates = array_merge([$workspace_id]$workspace_tree[$workspace_id]['ancestors']);
    }
    else {
      $workspace_candidates = [$workspace_id];
    }

    

  protected function getLangcodeTable(QueryPluginBase $query$relationship) {
    /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
    $storage = \Drupal::entityTypeManager()->getStorage($this->entityType->id());
    $langcode_key = $this->entityType->getKey('langcode');
    $langcode_table = $storage->getTableMapping()->getFieldTableName($langcode_key);

    // If the entity type is revisionable, we need to take into account views of     // entity revisions. Usually the view will use the entity data table as the     // query base table, however, in case of an entity revision view, we need to     // use the revision table or the revision data table, depending on which one     // is being used as query base table.     if ($this->entityType->isRevisionable()) {
      $query_base_table = $query->relationships[$relationship]['base'] ??
        $this->view->storage->get('base_table');
      $revision_table = $storage->getRevisionTable();
      $revision_data_table = $storage->getRevisionDataTable();
      
    $definitions['multivalued_base_field'] = BaseFieldDefinition::create('string')
      ->setName('multivalued_base_field')
      ->setTargetEntityTypeId('entity_test_mulrev')
      ->setTargetBundle('entity_test_mulrev')
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      // Base fields are non-translatable and non-revisionable by default, but       // we explicitly set these values here for extra clarity.       ->setTranslatable(FALSE)
      ->setRevisionable(FALSE);
    $this->state->set('entity_test_mulrev.additional_base_field_definitions', $definitions);

    $this->tableMapping = $this->entityTypeManager->getStorage('entity_test_mulrev')->getTableMapping();

    // Ensure that the tables for the new field are created.     $this->applyEntityUpdates('entity_test_mulrev');
  }

  /** * Tests DefaultTableMapping::getFieldTableName(). * * @covers ::getFieldTableName */
  public function testGetFieldTableName() {
    
        $entity_id_field = $entity_type->getKey('revision');
        // This contains the relevant SQL field to be used when joining field         // tables.         $field_id_field = 'revision_id';
      }
      else {
        $entity_id_field = $entity_type->getKey('id');
        $field_id_field = 'entity_id';
      }

      /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
      $table_mapping = $this->entityTypeManager->getStorage($entity_type_id)->getTableMapping();

      // Check whether this field is stored in a dedicated table.       if ($field_storage && $table_mapping->requiresDedicatedTableStorage($field_storage)) {
        $delta = NULL;

        if ($key < $count) {
          $next = $specifiers[$key + 1];
          // If this is a numeric specifier we're adding a condition on the           // specific delta.           if (is_numeric($next)) {
            $delta = $next;
            
  // restrictions to SELECT queries. hook_media_access() can be used to apply   // access control to 'update' and 'delete' operations.   if (!($query instanceof SelectInterface)) {
    return;
  }

  // The tables in the query. This can include media entity tables and other   // tables. Tables might be joined more than once, with aliases.   $query_tables = $query->getTables();

  // The tables belonging to media entity storage.   $table_mapping = \Drupal::entityTypeManager()->getStorage('media')->getTableMapping();
  $media_tables = $table_mapping->getTableNames();

  // For each table in the query, if it's a media entity storage table, add a   // condition to filter out records belonging to a media entity that we wish   // to hide.   foreach ($query_tables as $alias => $info) {
    // Skip over subqueries.     if ($info['table'] instanceof SelectInterface) {
      continue;
    }
    $real_table_name = $info['table'];
    
foreach ($tids as $tid) {
        $terms[$nid][$tid] = $all_terms[$tid];
      }
    }
    return $terms;
  }

  /** * {@inheritdoc} */
  public function getTermIdsWithPendingRevisions() {
    $table_mapping = $this->getTableMapping();
    $id_field = $table_mapping->getColumnNames($this->entityType->getKey('id'))['value'];
    $revision_field = $table_mapping->getColumnNames($this->entityType->getKey('revision'))['value'];
    $rta_field = $table_mapping->getColumnNames($this->entityType->getKey('revision_translation_affected'))['value'];
    $langcode_field = $table_mapping->getColumnNames($this->entityType->getKey('langcode'))['value'];
    $revision_default_field = $table_mapping->getColumnNames($this->entityType->getRevisionMetadataKey('revision_default'))['value'];

    $query = $this->database->select($this->getRevisionDataTable(), 'tfr');
    $query->fields('tfr', [$id_field]);
    $query->addExpression("MAX([tfr].[$revision_field])", $revision_field);

    $query->join($this->getRevisionTable(), 'tr', "[tfr].[$revision_field] = [tr].[$revision_field] AND [tr].[$revision_default_field] = 0");

    
Home | Imprint | This part of the site doesn't use cookies.