You are here

public static function OrderConnectStorage::insert in Basic cart 8

Same name and namespace in other branches
  1. 8.0 src/OrderConnectStorage.php \Drupal\basic_cart\OrderConnectStorage::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/OrderConnectStorage.php, line 33

Class

OrderConnectStorage
Class OrderConnectStorage.

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;
}