You are here

public static function CartStorage::insert in Basic cart 8

Same name and namespace in other branches
  1. 8.6 src/CartStorage.php \Drupal\basic_cart\CartStorage::insert()
  2. 8.0 src/CartStorage.php \Drupal\basic_cart\CartStorage::insert()
  3. 8.2 src/CartStorage.php \Drupal\basic_cart\CartStorage::insert()
  4. 8.3 src/CartStorage.php \Drupal\basic_cart\CartStorage::insert()
  5. 8.4 src/CartStorage.php \Drupal\basic_cart\CartStorage::insert()
  6. 8.5 src/CartStorage.php \Drupal\basic_cart\CartStorage::insert()

Save an entry in the database.

The underlying DBTNG function is db_insert().

Exception handling is shown in this example. It could be simplified without the try/catch blocks, but since an insert will throw an exception and terminate your application if the exception is not handled, it is best to employ try/catch.

Parameters

array $entry: An array containing all the fields of the database record.

Return value

int The number of updated rows.

Throws

\Exception When the database insert fails.

See also

db_insert()

File

src/CartStorage.php, line 33

Class

CartStorage
Class CartStorage.

Namespace

Drupal\basic_cart

Code

public static function insert($entry) {
  $return_value = NULL;
  try {
    $return_value = db_insert(self::TABLE)
      ->fields($entry)
      ->execute();
  } catch (\Exception $e) {
    drupal_set_message(t('db_insert failed. Message = %message, query= %query', array(
      '%message' => $e
        ->getMessage(),
      '%query' => $e->query_string,
    )), 'error');
  }
  return $return_value;
}