function update_body_format($table, $old, $new)
{
    // Get database connection
    \Drupal\Core\Database\Database::setActiveConnection();
    $connection = \Drupal\Core\Database\Database::getConnection();
    // Update the body_format for the specified table
    $results = $connection->update($table)->fields(array('body_format' => $new))->condition('body_format', $old, '=')->execute();
    return $results;
}
 /**
  * Loads a database fixture into the source database connection.
  *
  * @param string $path
  *   Path to the dump file.
  */
 protected function loadFixture($path)
 {
     $default_db = Database::getConnection()->getKey();
     Database::setActiveConnection($this->sourceDatabase->getKey());
     if (substr($path, -3) == '.gz') {
         $path = 'compress.zlib://' . $path;
     }
     require $path;
     Database::setActiveConnection($default_db);
 }
function update_field_format($field_name, $new)
{
    // Get database connection
    \Drupal\Core\Database\Database::setActiveConnection();
    $connection = \Drupal\Core\Database\Database::getConnection();
    $table = 'node__field_' . $field_name;
    $format_column = 'field_' . $field_name . '_format';
    // Update the format for the specified table
    $results = $connection->update($table)->fields(array($format_column => $new))->execute();
    return $results;
}
Example #4
0
 /**
  * Run the database script.
  *
  * @param \Drupal\Core\Database\Connection $connection
  *   Connection used by the script when included.
  * @param string $script
  *   Path to dump script.
  */
 protected function runScript(Connection $connection, $script)
 {
     $old_key = Database::setActiveConnection($connection->getKey());
     if (substr($script, -3) == '.gz') {
         $script = "compress.zlib://{$script}";
     }
     try {
         require $script;
     } catch (SchemaObjectExistsException $e) {
         throw new \RuntimeException('An existing Drupal installation exists at this location. Try removing all tables or changing the database prefix in your settings.php file.');
     }
     Database::setActiveConnection($old_key);
 }
function strip_html_tags_from_field($field_name)
{
    \Drupal\Core\Database\Database::setActiveConnection();
    $connection = \Drupal\Core\Database\Database::getConnection();
    $column_name = 'field_' . $field_name . '_value';
    $table_name = 'node__field_' . $field_name;
    $select_query = 'SELECT bundle, entity_id, revision_id, ' . $column_name . ' FROM ' . $table_name . ';';
    print "Updating value for " . $column_name . "...\n";
    $results = $connection->query($select_query)->fetchAll();
    $i = 0;
    if ($results) {
        foreach ($results as $record) {
            $new_value = strip_tags($record->{$column_name});
            $connection->update($table_name)->fields(array($column_name => $new_value))->condition('bundle', $record->bundle, '=')->condition('revision_id', $record->revision_id, '=')->condition('entity_id', $record->entity_id, '=')->execute();
            $i = $i + 1;
        }
    }
    print "Updated " . $i . " row(s)\n";
}
Example #6
0
 /**
  * Tests the findTables() method.
  */
 public function testFindTables()
 {
     // We will be testing with three tables, two of them using the default
     // prefix and the third one with an individually specified prefix.
     // Set up a new connection with different connection info.
     $connection_info = Database::getConnectionInfo();
     // Add per-table prefix to the second table.
     $new_connection_info = $connection_info['default'];
     $new_connection_info['prefix']['test_2_table'] = $new_connection_info['prefix']['default'] . '_shared_';
     Database::addConnectionInfo('test', 'default', $new_connection_info);
     Database::setActiveConnection('test');
     // Create the tables.
     $table_specification = ['description' => 'Test table.', 'fields' => ['id' => ['type' => 'int', 'default' => NULL]]];
     Database::getConnection()->schema()->createTable('test_1_table', $table_specification);
     Database::getConnection()->schema()->createTable('test_2_table', $table_specification);
     Database::getConnection()->schema()->createTable('the_third_table', $table_specification);
     // Check the "all tables" syntax.
     $tables = Database::getConnection()->schema()->findTables('%');
     sort($tables);
     $expected = ['config', 'test_1_table', 'test_2_table', 'the_third_table'];
     $this->assertEqual($tables, $expected, 'All tables were found.');
     // Check the restrictive syntax.
     $tables = Database::getConnection()->schema()->findTables('test_%');
     sort($tables);
     $expected = ['test_1_table', 'test_2_table'];
     $this->assertEqual($tables, $expected, 'Two tables were found.');
     // Go back to the initial connection.
     Database::setActiveConnection('default');
 }
Example #7
0
 public static final function setActiveConnection($key = 'default')
 {
     return BaseDatabase::setActiveConnection($key);
 }
Example #8
0
 /**
  * READ from the database using a filter array.
  *
  * @param string $table
  *   The table we are worrking on.
  *
  * @param array $entry
  *   An array containing all the fields used to search the entries in the
  *   table.
  *
  * @return object
  *   An object containing the loaded entries if found.
  */
 public static function load($table, $entry = array())
 {
     // switch database (cf settings.php)
     \Drupal\Core\Database\Database::setActiveConnection('external');
     // Build query
     $select = db_select($table, 't');
     $select->fields('t');
     foreach ($entry as $field => $value) {
         $select->condition($field, $value);
     }
     try {
         $result = $select->execute()->fetchAll();
     } catch (\Exception $e) {
         drupal_set_message(t('db_read failed. Message = %message, query= %query', array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
         return FALSE;
         self::logAndDsm('error', $e->getMessage, array($e->query_string));
         return FALSE;
     }
     \Drupal\Core\Database\Database::setActiveConnection();
     return $result;
 }
<?php

function load_user($user_id)
{
    return \Drupal\user\Entity\User::load($user_id);
}
// Get database connection
\Drupal\Core\Database\Database::setActiveConnection();
$connection = \Drupal\Core\Database\Database::getConnection();
// Get all the users
$results = $connection->query('SELECT u.uid, d.name, d.mail FROM users AS u INNER JOIN users_field_data AS d ON u.uid = d.uid;')->fetchAll();
// For each user, remove the user_picture and then save the user
if ($results) {
    foreach ($results as $record) {
        $user_id = $record->uid;
        $user_name = $record->name;
        $user_email = $record->mail;
        print "Removing user_picture from {$user_name} ({$user_email})...\n";
        $user = load_user($user_id);
        $user->set("user_picture", NULL);
        $user->save();
    }
}