You are here

function views_db_object::save_row in Views (for Drupal 7) 6.3

Same name and namespace in other branches
  1. 6.2 includes/view.inc \views_db_object::save_row()
  2. 7.3 includes/view.inc \views_db_object::save_row()

Write the row to the database.

Parameters

$update: If true this will be an UPDATE query. Otherwise it will be an INSERT.

1 call to views_db_object::save_row()
view::save in includes/view.inc
Save the view to the database. If the view does not already exist, A vid will be assigned to the view and also returned from this function.

File

includes/view.inc, line 2023
view.inc Provides the view object type and associated methods.

Class

views_db_object
Base class for views' database objects.

Code

function save_row($update = NULL) {
  $schema = drupal_get_schema($this->db_table);
  $fields = $defs = $values = $serials = array();

  // Go through our schema and build correlations.
  foreach ($schema['fields'] as $field => $info) {

    // special case -- skip serial types if we are updating.
    if ($info['type'] == 'serial') {
      $serials[] = $field;
      continue;
    }
    $fields[] = $field;
    switch ($info['type']) {
      case 'blob':
        $defs[] = '%b';
        break;
      case 'int':
        $defs[] = '%d';
        break;
      case 'float':
      case 'numeric':
        $defs[] = '%f';
        break;
      default:
        $defs[] = "'%s'";
    }
    if (empty($info['serialize'])) {
      $values[] = $this->{$field};
    }
    else {
      $values[] = serialize($this->{$field});
    }
  }
  $query = '';
  if (!$update) {
    $query = "INSERT INTO {" . $this->db_table . "} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $defs) . ')';
  }
  else {
    $query = '';
    foreach ($fields as $id => $field) {
      if ($query) {
        $query .= ', ';
      }
      $query .= $field . ' = ' . $defs[$id];
    }
    $query = "UPDATE {" . $this->db_table . "} SET " . $query . " WHERE {$update} = " . $this->{$update};
  }
  db_query($query, $values);
  if ($serials && !$update) {

    // get last insert ids and fill them in.
    foreach ($serials as $field) {
      $this->{$field} = db_last_insert_id($this->db_table, $field);
    }
  }
}