getDedicatedRevisionTableName example

->orderBy('revision_id', 'ASC')
      ->orderBy('langcode', 'ASC')
      ->execute()
      ->fetchAll(\PDO::FETCH_ASSOC);
    $this->assertSameSize($expected$result);

    // Use assertEquals and not assertSame here to prevent that a different     // sequence of the columns in the table will affect the check.     $this->assertEquals($expected$result);

    if ($create_entity_revision) {
      $dedicated_deleted_revision_table_name = $table_mapping->getDedicatedRevisionTableName($storage_definition, TRUE);
      $this->assertTrue($schema_handler->tableExists($dedicated_deleted_revision_table_name), 'A dedicated revision table was created for the deleted new_base_field.');

      if ($base_field_revisionable) {
        $expected[] = [
          'bundle' => $entity->bundle(),
          'deleted' => '1',
          'entity_id' => '2',
          'revision_id' => '3',
          'langcode' => 'en',
          'delta' => '0',
          'new_base_field_value' => 'bar',
        ];
'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 = [
      'table' => $current_table,
      
$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;
  }

  /** * Gets the base table name. * * @return string * The base table name. * * @internal */

  protected function assertBundleFieldSchema(bool $revisionable): void {
    $entity_type_id = 'entity_test_update';
    $field_storage_definition = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id)['new_bundle_field'];
    $database_schema = $this->database->schema();
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->getTableMapping();
    $this->assertTrue($database_schema->tableExists($table_mapping->getDedicatedDataTableName($field_storage_definition)));
    if ($revisionable) {
      $this->assertTrue($database_schema->tableExists($table_mapping->getDedicatedRevisionTableName($field_storage_definition)));
    }
  }

  /** * Asserts that the backup tables have been kept after a successful update. * * @internal */
  protected function assertBackupTables(): void {
    $backups = \Drupal::keyValue('entity.update_backup')->getAll();
    $backup = reset($backups);

    
$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')];

    
// Any dedicated field table is a candidate.       if ($field_name = array_search($table_info['table']$dedicated_field_data_tables, TRUE)) {
        $relationship = $table_info['relationship'];

        // There can be reverse relationships used. If so, Workspaces can't do         // anything with them. Detect this and skip.         if ($table_info['join']->field != 'entity_id') {
          continue;
        }

        // Get the dedicated revision table name.         $new_table_name = $table_mapping->getDedicatedRevisionTableName($field_storage_definitions[$field_name]);

        // Now add the workspace_association table.         $workspace_association_table = $this->ensureWorkspaceAssociationTable($entity_type->id()$query$relationship);

        // Update the join to use our COALESCE.         $revision_field = $entity_type->getKey('revision');
        $table_info['join']->leftFormula = "COALESCE($workspace_association_table.target_entity_revision_id, $relationship.$revision_field)";

        // Update the join and the table info to our new table name, and to join         // on the revision key.         $table_info['table'] = $new_table_name;
        
$this->assertEquals($expected$this->tableMapping->getAllFieldTableNames('multivalued_base_field'));
  }

  /** * Tests DefaultTableMapping::getTableNames(). * * @covers ::getTableNames */
  public function testGetTableNames() {
    $storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('entity_test_mulrev');
    $dedicated_data_table = $this->tableMapping->getDedicatedDataTableName($storage_definitions['multivalued_base_field']);
    $dedicated_revision_table = $this->tableMapping->getDedicatedRevisionTableName($storage_definitions['multivalued_base_field']);

    // Check that both the data and the revision tables exist for a multi-valued     // base field.     $database_schema = \Drupal::database()->schema();
    $this->assertTrue($database_schema->tableExists($dedicated_data_table));
    $this->assertTrue($database_schema->tableExists($dedicated_revision_table));

    // Check that the table mapping contains both the data and the revision     // tables exist for a multi-valued base field.     $expected = [
      'entity_test_mulrev',
      
foreach ($definitions[$bundle] as $field_name => $field_definition) {
        $storage_definition = $field_definition->getFieldStorageDefinition();
        if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
          $storage_definitions[$field_name] = $storage_definition;
        }
      }
    }

    // Load field data.     $langcodes = array_keys($this->languageManager->getLanguages(LanguageInterface::STATE_ALL));
    foreach ($storage_definitions as $field_name => $storage_definition) {
      $table = !$load_from_revision ? $table_mapping->getDedicatedDataTableName($storage_definition) : $table_mapping->getDedicatedRevisionTableName($storage_definition);

      // Ensure that only values having valid languages are retrieved. Since we       // are loading values for multiple entities, we cannot limit the query to       // the available translations.       $results = $this->database->select($table, 't')
        ->fields('t')
        ->condition(!$load_from_revision ? 'entity_id' : 'revision_id', $ids, 'IN')
        ->condition('deleted', 0)
        ->condition('langcode', $langcodes, 'IN')
        ->orderBy('delta')
        ->execute();

      

  private function getTableNames(EntityTypeInterface $entity_type, array $field_storage_definitions, TableMappingInterface $table_mapping) {
    $table_names = $table_mapping->getTableNames();
    if ($table_mapping instanceof DefaultTableMapping && $entity_type->isRevisionable()) {
      foreach ($field_storage_definitions as $storage_definition) {
        if ($table_mapping->requiresDedicatedTableStorage($storage_definition)) {
          $dedicated_revision_table_name = $table_mapping->getDedicatedRevisionTableName($storage_definition);
          if (!$storage_definition->isRevisionable() && !in_array($dedicated_revision_table_name$table_names)) {
            $table_names[] = $dedicated_revision_table_name;
          }
        }
      }
    }
    return $table_names;
  }

  /** * {@inheritdoc} */

  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. */
$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],
          ],
        ];

        if ($revisionable) {
          $revision_table = $table_mapping->getDedicatedRevisionTableName($field_definition->getFieldStorageDefinition());

          $data[$revision_table]['table']['group'] = $this->t('@entity_type revision', ['@entity_type' => $this->entityType->getLabel()]);
          $data[$revision_table]['table']['provider'] = $this->entityType->getProvider();
          $data[$revision_table]['table']['join'][$views_revision_base_table] = [
            'left_field' => $revision_field,
            'field' => 'entity_id',
            'extra' => [
              ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE],
            ],
          ];
        }
      }
->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. */
  public function providerTestGetDedicatedTableName() {
    
Home | Imprint | This part of the site doesn't use cookies.