You are here

public function DbtngExampleRepository::update in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/dbtng_example/src/DbtngExampleRepository.php \Drupal\dbtng_example\DbtngExampleRepository::update()

Update an entry in the database.

Parameters

array $entry: An array containing all the fields of the item to be updated.

Return value

int The number of updated rows.

File

dbtng_example/src/DbtngExampleRepository.php, line 96

Class

DbtngExampleRepository
Repository for database-related helper methods for our example.

Namespace

Drupal\dbtng_example

Code

public function update(array $entry) {
  try {

    // Connection->update()...->execute() returns the number of rows updated.
    $count = $this->connection
      ->update('dbtng_example')
      ->fields($entry)
      ->condition('pid', $entry['pid'])
      ->execute();
  } catch (\Exception $e) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Update failed. Message = %message, query= %query', [
      '%message' => $e
        ->getMessage(),
      '%query' => $e->query_string,
    ]), 'error');
  }
  return $count ?? 0;
}