getDedicatedDataTableName example

$edit = [
        'field_string' => [(['value' => $this->randomMachineName()])],
      ];
      $nodes[] = Node::create(['type' => 'page'] + $edit);
    }

    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->getTableMapping();

    $current_table = $table_mapping->getDedicatedDataTableName($field_storage_string);
    $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage_string);
    $data = $this->getViewsData();

    $this->assertArrayHasKey($current_table$data);
    $this->assertArrayHasKey($revision_table$data);

    // The node field should join against node_field_data.     $this->assertArrayHasKey('node_field_data', $data[$current_table]['table']['join']);
    $this->assertArrayHasKey('node_field_revision', $data[$revision_table]['table']['join']);

    $expected_join = [
      

  protected function getDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition, ContentEntityTypeInterface $entity_type = NULL) {
    $dedicated_table_schema = parent::getDedicatedTableSchema($storage_definition$entity_type);

    // Add an index on 'bundle', 'delta' and 'parent_target_id' columns to     // increase the performance of the query from     // \Drupal\taxonomy\TermStorage::getVocabularyHierarchyType().     if ($storage_definition->getName() === 'parent') {
      /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
      $table_mapping = $this->storage->getTableMapping();
      $dedicated_table_name = $table_mapping->getDedicatedDataTableName($storage_definition);

      unset($dedicated_table_schema[$dedicated_table_name]['indexes']['bundle']);
      $dedicated_table_schema[$dedicated_table_name]['indexes']['bundle_delta_target_id'] = [
        'bundle',
        'delta',
        $table_mapping->getFieldColumnName($storage_definition, 'target_id'),
      ];
    }

    return $dedicated_table_schema;
  }

}
protected function assertFieldStorageLangcode(FieldableEntityInterface $entity, string $message = ''): void {
    $status = TRUE;
    $entity_type = $entity->getEntityTypeId();
    $id = $entity->id();
    $langcode = $entity->getUntranslated()->language()->getId();
    $fields = [$this->fieldName, $this->untranslatableFieldName];
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = \Drupal::entityTypeManager()->getStorage($entity_type)->getTableMapping();

    foreach ($fields as $field_name) {
      $field_storage = FieldStorageConfig::loadByName($entity_type$field_name);
      $table = $table_mapping->getDedicatedDataTableName($field_storage);

      $record = \Drupal::database()
        ->select($table, 'f')
        ->fields('f')
        ->condition('f.entity_id', $id)
        ->condition('f.revision_id', $id)
        ->execute()
        ->fetchObject();

      if ($record->langcode != $langcode) {
        $status = FALSE;
        
$pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id;
  $table_mapping = \Drupal::entityTypeManager()->getStorage($entity_type_id)->getTableMapping();

  [$label] = views_entity_field_label($entity_type_id$field_name);

  $data['file_managed'][$pseudo_field_name]['relationship'] = [
    'title' => t('@entity using @field', ['@entity' => $entity_type->getLabel(), '@field' => $label]),
    'help' => t('Relate each @entity with a @field set to the image.', ['@entity' => $entity_type->getLabel(), '@field' => $label]),
    'id' => 'entity_reverse',
    'field_name' => $field_name,
    'entity_type' => $entity_type_id,
    'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
    'field field' => $field_name . '_target_id',
    'base' => $entity_type->getBaseTable(),
    'base field' => $entity_type->getKey('id'),
    'label' => $field_name,
    'join_extra' => [
      0 => [
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ],
    ],
  ];
$entity = EntityTest::create();
      $entity->field_int[] = mt_rand(1, 99);
      $entity->field_int[] = mt_rand(1, 99);
      $entity->name[] = $this->randomMachineName();
      $entity->save();
    }

    $storage = \Drupal::entityTypeManager()->getStorage('entity_test');
    if ($storage instanceof SqlContentEntityStorage) {
      // Count the actual number of rows in the field table.       $table_mapping = $storage->getTableMapping();
      $field_table_name = $table_mapping->getDedicatedDataTableName($field_storage);
      $result = Database::getConnection()->select($field_table_name, 't')
        ->fields('t')
        ->countQuery()
        ->execute()
        ->fetchField();
      $this->assertEquals(24, $result, 'The field table has 24 rows.');
    }

    $this->assertTrue($field_storage->hasdata(), 'There are entities with field data.');
    $this->assertEquals(12, $this->storage->countFieldData($field_storage), 'There are 12 entities with field data.');

    

    $this->fieldStorage->save();
    $this->field = FieldConfig::create([
      'field_storage' => $this->fieldStorage,
      'bundle' => $entity_type,
    ]);
    $this->field->save();

    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = \Drupal::entityTypeManager()->getStorage($entity_type)->getTableMapping();
    $this->tableMapping = $table_mapping;
    $this->table = $table_mapping->getDedicatedDataTableName($this->fieldStorage);
    $this->revisionTable = $table_mapping->getDedicatedRevisionTableName($this->fieldStorage);
  }

  /** * Tests field loading works correctly by inserting directly in the tables. */
  public function testFieldLoad() {
    $entity_type = $bundle = 'entity_test_rev';
    $storage = $this->container->get('entity_type.manager')->getStorage($entity_type);

    $columns = ['bundle', 'deleted', 'entity_id', 'revision_id', 'delta', 'langcode', $this->tableMapping->getFieldColumnName($this->fieldStorage, 'value')];

    


    // Remove the base field and apply updates.     $this->removeBaseField($entity_type_id);
    $this->applyEntityUpdates();

    // Check that the base field's column is deleted.     $this->assertFalse($schema_handler->fieldExists($entity_type_id, 'new_base_field'), 'Column deleted from shared table for new_base_field.');

    // Check that a dedicated 'deleted' table was created for the deleted base     // field.     $dedicated_deleted_table_name = $table_mapping->getDedicatedDataTableName($storage_definition, TRUE);
    $this->assertTrue($schema_handler->tableExists($dedicated_deleted_table_name), 'A dedicated table was created for the deleted new_base_field.');

    $expected[] = [
      'bundle' => $entity->bundle(),
      'deleted' => '1',
      'entity_id' => '2',
      'revision_id' => '2',
      'langcode' => 'en',
      'delta' => '0',
      'new_base_field_value' => 'foo',
    ];

    
$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
      ->getQuery()
      

  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) {
      // If we reach the workspace_association array item before any candidates,       // then we do not need to move it.       if ($table_info['table'] == 'workspace_association') {
        break;
      }

      
$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();
    entity_test_delete_bundle('custom', 'entity_test_update');

    
$this->entityType
      ->expects($this->any())
      ->method('isTranslatable')
      ->willReturn(FALSE);
    $this->entityType
      ->expects($this->any())
      ->method('isRevisionable')
      ->willReturn(FALSE);

    $table_mapping = new TestDefaultTableMapping($this->entityType, []$info['prefix']);

    $this->assertSame($expected_data_table$table_mapping->getDedicatedDataTableName($definition));
    $this->assertSame($expected_revision_table$table_mapping->getDedicatedRevisionTableName($definition));
  }

  /** * Provides test data for testGetDedicatedTableName(). * * @return array[] * A nested array where each inner array has the following values: an array * consisting of the entity type ID, field name and a table prefix, followed * by the expected data table name and the revision table name. */
  

  protected function ensureFieldTable($index_prefix, &$field$type$langcode$base_table$entity_id_field$field_id_field$delta) {
    $field_name = $field->getName();
    if (!isset($this->fieldTables[$index_prefix . $field_name])) {
      $entity_type_id = $this->sqlQuery->getMetaData('entity_type');
      /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
      $table_mapping = $this->entityTypeManager->getStorage($entity_type_id)->getTableMapping();
      $table = !$this->sqlQuery->getMetaData('all_revisions') ? $table_mapping->getDedicatedDataTableName($field) : $table_mapping->getDedicatedRevisionTableName($field);
      if ($field->getCardinality() != 1) {
        $this->sqlQuery->addMetaData('simple_query', FALSE);
      }
      $this->fieldTables[$index_prefix . $field_name] = $this->addJoin($type$table, "[%alias].[$field_id_field] = [$base_table].[$entity_id_field]", $langcode$delta);
    }
    return $this->fieldTables[$index_prefix . $field_name];
  }

  /** * Adds a join to a given table. * * @param string $type * The join type. * @param string $table * The table to join to. * @param string $join_condition * The condition on which to join to. * @param string $langcode * The langcode we use on the join. * @param string|null $delta * (optional) A delta which should be used as additional condition. * * @return string * Returns the alias of the joined table. */
return $table_mapping->requiresDedicatedTableStorage($definition);
    });
    $extra_columns = [
      'bundle',
      'deleted',
      'entity_id',
      'revision_id',
      'langcode',
      'delta',
    ];
    foreach ($dedicated_table_definitions as $field_name => $definition) {
      $tables = [$table_mapping->getDedicatedDataTableName($definition)];
      if ($revisionable && $definition->isRevisionable()) {
        $tables[] = $table_mapping->getDedicatedRevisionTableName($definition);
      }
      foreach ($tables as $table_name) {
        $table_mapping->setFieldNames($table_name[$field_name]);
        $table_mapping->setExtraColumns($table_name$extra_columns);
      }
    }

    return $table_mapping;
  }

  
// There's nothing else we can do if the field storage has a custom storage.     if ($storage_definition->hasCustomStorage()) {
      return;
    }

    $table_mapping = $this->getTableMapping($this->entityType, [$storage_definition]);
    $field_table_name = $table_mapping->getFieldTableName($storage_definition->getName());

    if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
      // Move the table to a unique name while the table contents are being       // deleted.       $table = $table_mapping->getDedicatedDataTableName($storage_definition);
      $new_table = $table_mapping->getDedicatedDataTableName($storage_definition, TRUE);
      $this->database->schema()->renameTable($table$new_table);
      if ($this->entityType->isRevisionable()) {
        $revision_table = $table_mapping->getDedicatedRevisionTableName($storage_definition);
        $revision_new_table = $table_mapping->getDedicatedRevisionTableName($storage_definition, TRUE);
        $this->database->schema()->renameTable($revision_table$revision_new_table);
      }
    }
    else {
      // Move the field data from the shared table to a dedicated one in order       // to allow it to be purged like any other field.
        // that are on both base and data tables, only add them on the data         // table (same for revision vs. revision data).         if ($data_table && ($table === $base_table || $table === $revision_table) && in_array($field_name$duplicate_fields)) {
          continue;
        }
        $this->mapFieldDefinition($table$field_name$field_definitions[$field_name]$table_mapping$data[$table]);
      }
    }

    foreach ($field_definitions as $field_definition) {
      if ($table_mapping->requiresDedicatedTableStorage($field_definition->getFieldStorageDefinition())) {
        $table = $table_mapping->getDedicatedDataTableName($field_definition->getFieldStorageDefinition());

        $data[$table]['table']['group'] = $this->entityType->getLabel();
        $data[$table]['table']['provider'] = $this->entityType->getProvider();
        $data[$table]['table']['join'][$views_base_table] = [
          'left_field' => $entity_id_key,
          'field' => 'entity_id',
          'extra' => [
            ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
          ],
        ];

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