Drupal 8 - aggiungere una tabella al DB tramite hook_update_N()

                      
                        
    

Drupal 8 - aggiungere una tabella al DB tramite hook_update_N()

Nel file miomodulo.install è possibile utilizzare la funzione miomodulo_update_8101() per effettuare operazioni che saranno richiamate durante l'upgrade del vostro modulo.

ad esempio:

function miomodulo_update_8101() {
  $database = \Drupal::database();
  $schema = $database->schema();
  $table_name = 'my_table';
  $table_schema = [
    'fields' => [
      'sid' => [
        'type' => 'serial',
        'size' => 'big',
        'not null' => TRUE,
      ],
      'nid' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'action' => [
        'type' => 'varchar',
        'not null' => TRUE,
        'length' => 25,
      ],
      'uids' => [
        'type' => 'blob',
        'not null' => TRUE,
      ],
    ],
    'primary key' => ['sid'],
  ];
  $schema->createTable($table_name, $table_schema);
}