function UserStorage::doSaveFieldItems

Same name and namespace in other branches
  1. 9 core/modules/user/src/UserStorage.php \Drupal\user\UserStorage::doSaveFieldItems()
  2. 8.9.x core/modules/user/src/UserStorage.php \Drupal\user\UserStorage::doSaveFieldItems()
  3. 11.x core/modules/user/src/UserStorage.php \Drupal\user\UserStorage::doSaveFieldItems()

Overrides SqlContentEntityStorage::doSaveFieldItems

File

core/modules/user/src/UserStorage.php, line 20

Class

UserStorage
Controller class for users.

Namespace

Drupal\user

Code

protected function doSaveFieldItems(ContentEntityInterface $entity, array $names = []) {
  // The anonymous user account is saved with the fixed user ID of 0. MySQL
  // does not support inserting an ID of 0 into serial field unless the SQL
  // mode is set to NO_AUTO_VALUE_ON_ZERO.
  // @todo https://drupal.org/i/3222123 implement a generic fix for all entity
  //   types.
  if ($entity->id() === 0) {
    $database = \Drupal::database();
    if ($database->databaseType() === 'mysql') {
      $sql_mode = $database->query("SELECT @@sql_mode;")
        ->fetchField();
      $database->query("SET sql_mode = '{$sql_mode},NO_AUTO_VALUE_ON_ZERO'");
    }
  }
  parent::doSaveFieldItems($entity, $names);
  // Reset the SQL mode if we've changed it.
  if (isset($sql_mode, $database)) {
    $database->query("SET sql_mode = '{$sql_mode}'");
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.