You are here

public function DatabaseStorage::setIfNotExists in Service Container 7.2

Same name and namespace in other branches
  1. 7 lib/Drupal/Core/KeyValueStore/DatabaseStorage.php \Drupal\Core\KeyValueStore\DatabaseStorage::setIfNotExists()

Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::setIfNotExists().

Overrides KeyValueStoreInterface::setIfNotExists

File

lib/Drupal/Core/KeyValueStore/DatabaseStorage.php, line 124
Contains \Drupal\Core\KeyValueStore\DatabaseStorage.

Class

DatabaseStorage
Defines a default key/value store implementation.

Namespace

Drupal\Core\KeyValueStore

Code

public function setIfNotExists($key, $value) {
  $result = $this->connection
    ->merge($this->table)
    ->insertFields(array(
    'collection' => $this->collection,
    'name' => $key,
    'value' => $this->serializer
      ->encode($value),
  ))
    ->condition('collection', $this->collection)
    ->condition('name', $key)
    ->execute();
  return $result == Merge::STATUS_INSERT;
}