getRevisionTable example

// If 'progress' is not set, then this will be the first run of the batch.     if (!isset($sandbox['progress'])) {
      $sandbox['progress'] = 0;
      $sandbox['current_id'] = -1;
    }

    // If the original entity type is revisionable, we need to copy all the     // revisions.     $load_revisions = $original->isRevisionable();
    if ($load_revisions) {
      $table_name = $original->getRevisionTable();
      $identifier_field = $revision_id_key;
    }
    else {
      $table_name = $original->getBaseTable();
      $identifier_field = $id_key;
    }

    // Get the next entity identifiers to migrate.     // @todo Use an entity query when it is able to use the last installed     // entity type and field storage definitions.     // @see https://www.drupal.org/project/drupal/issues/2554235
/** * Prepares the basic query with proper metadata/tags and base fields. * * @return $this * Returns the called object. * * @throws \Drupal\Core\Entity\Query\QueryException * Thrown if the base table does not exist. */
  protected function prepare() {
    if ($this->allRevisions) {
      if (!$base_table = $this->entityType->getRevisionTable()) {
        throw new QueryException("No revision table for " . $this->entityTypeId . ", invalid query.");
      }
    }
    else {
      if (!$base_table = $this->entityType->getBaseTable()) {
        throw new QueryException("No base table for " . $this->entityTypeId . ", invalid query.");
      }
    }
    $simple_query = TRUE;
    if ($this->entityType->getDataTable()) {
      $simple_query = FALSE;
    }
->method('isRevisionable')
      ->willReturn(TRUE);
    $this->entityType->expects($this->once())
      ->method('getRevisionTable')
      ->willReturn($revision_table);
    $this->entityType->expects($this->any())
      ->method('getRevisionMetadataKeys')
      ->willReturn([]);

    $this->setUpEntityStorage();

    $this->assertSame($expected$this->entityStorage->getRevisionTable());
  }

  /** * Provides test data for testGetRevisionTable(). * * @return array[] * A nested array where each inner array has the revision table to be * returned by the mocked entity type as the first value and the expected * return value of SqlContentEntityStorage::getRevisionTable() as the * second value. */
  

class NodeStorage extends SqlContentEntityStorage implements NodeStorageInterface {

  /** * {@inheritdoc} */
  public function revisionIds(NodeInterface $node) {
    return $this->database->query(
      'SELECT [vid] FROM {' . $this->getRevisionTable() . '} WHERE [nid] = :nid ORDER BY [vid]',
      [':nid' => $node->id()]
    )->fetchCol();
  }

  /** * {@inheritdoc} */
  public function userRevisionIds(AccountInterface $account) {
    return $this->database->query(
      'SELECT [vid] FROM {' . $this->getRevisionDataTable() . '} WHERE [uid] = :uid ORDER BY [vid]',
      [':uid' => $account->id()]
    )

      // The field is stored in a shared table.       else {
        // ensureEntityTable() decides whether an entity property will be         // queried from the data table or the base table based on where it         // finds the property first. The data table is preferred, which is why         // it gets added before the base table.         $entity_tables = [];
        $revision_table = NULL;
        if ($query_revisions) {
          $data_table = $entity_type->getRevisionDataTable();
          $entity_base_table = $entity_type->getRevisionTable();
        }
        else {
          $data_table = $entity_type->getDataTable();
          $entity_base_table = $entity_type->getBaseTable();

          if ($field_storage && $field_storage->isRevisionable() && in_array($field_storage->getName()$entity_type->getRevisionMetadataKeys())) {
            $revision_table = $entity_type->getRevisionTable();
          }
        }
        if ($data_table) {
          $this->sqlQuery->addMetaData('simple_query', FALSE);
          
$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");

    $inner_select = $this->database->select($this->getRevisionDataTable(), 't');
    $inner_select->condition("t.$rta_field", '1');
    $inner_select->fields('t', [$id_field$langcode_field]);
    $inner_select->addExpression("MAX([t].[$revision_field])", $revision_field);
    $inner_select
      ->groupBy("t.$id_field")
      ->groupBy("t.$langcode_field");

    $query->join($inner_select, 'mr', "[mlfr].[$revision_field] = [mr].[$revision_field] AND [mlfr].[$langcode_field] = [mr].[$langcode_field]");

    
$all = $storage->loadMultiple();
    $this->assertEmpty($all, "All entities of type '$entity_type' should have been deleted.");

    // Verify that all data got deleted.     $definition = \Drupal::entityTypeManager()->getDefinition($entity_type);
    $connection = Database::getConnection();
    $this->assertEquals(0, (int) $connection->select($definition->getBaseTable())->countQuery()->execute()->fetchField(), 'Base table was emptied');

    if ($data_table = $definition->getDataTable()) {
      $this->assertEquals(0, (int) $connection->select($data_table)->countQuery()->execute()->fetchField(), 'Data table was emptied');
    }
    if ($revision_table = $definition->getRevisionTable()) {
      $this->assertEquals(0, (int) $connection->select($revision_table)->countQuery()->execute()->fetchField(), 'Revision table was emptied');
    }
    if ($revision_data_table = $definition->getRevisionDataTable()) {
      $this->assertEquals(0, (int) $connection->select($revision_data_table)->countQuery()->execute()->fetchField(), 'Revision data table was emptied');
    }

    // Test deleting a list of entities not indexed by entity id.     $entities = [];
    $entity = $storage->create(['name' => 'test', 'user_id' => $user1->id()]);
    $entity->save();
    $entities['test'] = $entity;
    

  protected function assertItemsTableCount(int $count, EntityTypeInterface $definition): void {
    $connection = Database::getConnection();
    $this->assertEquals(1, (int) $connection->select($definition->getBaseTable())->countQuery()->execute()->fetchField());
    $this->assertEquals(1, (int) $connection->select($definition->getDataTable())->countQuery()->execute()->fetchField());
    $this->assertEquals($count(int) $connection->select($definition->getRevisionTable())->countQuery()->execute()->fetchField());
    $this->assertEquals($count(int) $connection->select($definition->getRevisionDataTable())->countQuery()->execute()->fetchField());

  }

  /** * Creates a new revision in the entity of this test class. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity where revision will be created. * @param \Drupal\user\UserInterface $user * The author of the new revision. * @param int $timestamp * The timestamp of the new revision. * @param string $log_message * The log message of the new revision. */
$entity_tables[$base_table_data['table']['entity type']]['provider'] = $base_table_data['table']['provider'];
      }
    }

    // Include all relationships.     foreach ((array) $this->view->relationship as $relationship_id => $relationship) {
      $table_data = $views_data->get($relationship->definition['base']);
      if (isset($table_data['table']['entity type'])) {

        // If this is not one of the entity base tables, skip it.         $entity_type = \Drupal::entityTypeManager()->getDefinition($table_data['table']['entity type']);
        $entity_base_tables = [$entity_type->getBaseTable()$entity_type->getDataTable()$entity_type->getRevisionTable()$entity_type->getRevisionDataTable()];
        if (!in_array($relationship->definition['base']$entity_base_tables)) {
          continue;
        }

        $entity_tables[$relationship_id . '__' . $relationship->tableAlias] = [
          'base' => $relationship->definition['base'],
          'relationship_id' => $relationship_id,
          'alias' => $relationship->alias,
          'entity_type' => $table_data['table']['entity type'],
          'revision' => $table_data['table']['entity revision'],
        ];

        

  protected function ensureRevisionTable(EntityTypeInterface $entity_type, Sql $query$relationship) {
    // Get the alias for the 'workspace_association' table we chain off of in     // the COALESCE.     $workspace_association_table = $this->ensureWorkspaceAssociationTable($entity_type->id()$query$relationship);

    // Get the name of the revision table and revision key.     $base_revision_table = $entity_type->isTranslatable() ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
    $revision_field = $entity_type->getKey('revision');

    // If the table was already added and has a join against the same field on     // the revision table, reuse that rather than adding a new join.     if (isset($query->tables[$relationship][$base_revision_table])) {
      $table_queue =& $query->getTableQueue();
      $alias = $query->tables[$relationship][$base_revision_table]['alias'];
      if (isset($table_queue[$alias]['join']->field) && $table_queue[$alias]['join']->field == $revision_field) {
        // If this table previously existed, but was not added by us, we need         // to modify the join and make sure that 'workspace_association' comes         // first.
$data[$table]['moderation_state'] = [
        'title' => t('Moderation state'),
        'field' => [
          'id' => 'moderation_state_field',
          'default_formatter' => 'content_moderation_state',
          'field_name' => 'moderation_state',
        ],
        'filter' => ['id' => 'moderation_state_filter', 'allow empty' => TRUE],
        'sort' => ['id' => 'moderation_state_sort'],
      ];

      $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
      $data[$revision_table]['moderation_state'] = [
        'title' => t('Moderation state'),
        'field' => [
          'id' => 'moderation_state_field',
          'default_formatter' => 'content_moderation_state',
          'field_name' => 'moderation_state',
        ],
        'filter' => ['id' => 'moderation_state_filter', 'allow empty' => TRUE],
        'sort' => ['id' => 'moderation_state_sort'],
      ];
    }

    
->setProvider($entity_type->getProvider());

    // Build up a map of expected primary keys depending on the entity type     // configuration.     $id_key = $entity_type->getKey('id');
    $revision_key = $entity_type->getKey('revision');
    $langcode_key = $entity_type->getKey('langcode');

    $expected = [];
    $expected[$entity_type->getBaseTable()] = [$id_key];
    if ($entity_type->isRevisionable()) {
      $expected[$entity_type->getRevisionTable()] = [$revision_key];
    }
    if ($entity_type->isTranslatable()) {
      $expected[$entity_type->getDataTable()] = [$id_key$langcode_key];
    }
    if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
      $expected[$entity_type->getRevisionDataTable()] = [$revision_key$langcode_key];
    }

    // First, test explicitly deleting and re-installing a field. Make sure that     // all primary keys are there to start with.     $this->assertSame($expected$this->findPrimaryKeys($entity_type));

    

  public function __construct(ContentEntityTypeInterface $entity_type, array $storage_definitions$prefix = '') {
    $this->entityType = $entity_type;
    $this->fieldStorageDefinitions = $storage_definitions;
    $this->prefix = $prefix;

    // @todo Remove table names from the entity type definition in     // https://www.drupal.org/node/2232465.     $this->baseTable = $this->prefix . $entity_type->getBaseTable() ?: $entity_type->id();
    if ($entity_type->isRevisionable()) {
      $this->revisionTable = $this->prefix . $entity_type->getRevisionTable() ?: $entity_type->id() . '_revision';
    }
    if ($entity_type->isTranslatable()) {
      $this->dataTable = $this->prefix . $entity_type->getDataTable() ?: $entity_type->id() . '_field_data';
    }
    if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
      $this->revisionDataTable = $this->prefix . $entity_type->getRevisionDataTable() ?: $entity_type->id() . '_field_revision';
    }
  }

  /** * Initializes the table mapping. * * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type * The entity type definition. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface[] $storage_definitions * A list of field storage definitions that should be available for the * field columns of this table mapping. * @param string $prefix * (optional) A prefix to be used by all the tables of this mapping. * Defaults to an empty string. * * @return static * * @internal */
    $this->tableMapping = NULL;
    $this->revisionKey = NULL;
    $this->revisionTable = NULL;
    $this->dataTable = NULL;
    $this->revisionDataTable = NULL;

    $table_mapping = $this->getTableMapping();
    $this->baseTable = $table_mapping->getBaseTable();
    $revisionable = $this->entityType->isRevisionable();
    if ($revisionable) {
      $this->revisionKey = $this->entityType->getKey('revision') ?: 'revision_id';
      $this->revisionTable = $table_mapping->getRevisionTable();
    }
    $translatable = $this->entityType->isTranslatable();
    if ($translatable) {
      $this->dataTable = $table_mapping->getDataTable();
      $this->langcodeKey = $this->entityType->getKey('langcode');
      $this->defaultLangcodeKey = $this->entityType->getKey('default_langcode');
    }
    if ($revisionable && $translatable) {
      $this->revisionDataTable = $table_mapping->getRevisionDataTable();
    }
  }

  
$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();
      if ($query_base_table === $revision_table) {
        $langcode_table = $revision_table;
      }
      elseif ($query_base_table === $revision_data_table) {
        $langcode_table = $revision_data_table;
      }
    }

    return $langcode_table;
  }

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