You are here

protected function UserStorage::doSaveFieldItems in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/src/UserStorage.php \Drupal\user\UserStorage::doSaveFieldItems()

Writes entity field values to the storage.

This method is responsible for allocating entity and revision identifiers and updating the entity object with their values.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity object.

string[] $names: (optional) The name of the fields to be written to the storage. If an empty value is passed all field values are saved.

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}'");
  }
}