function view::save

3.x view.inc view::save()
2.x view.inc view::save()

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 1811
Provides the view object type and associated methods.

Class

view
An object to contain all of the data to generate a view, plus the member functions to build the view query, execute the query and render the output.

Code

function save() {
  if ($this->vid == 'new') {
    $this->vid = NULL;
  }
  // If there is no vid, check if a view with this machine name already exists.
  elseif (empty($this->vid)) {
    $vid = db_query("SELECT vid from {views_view} WHERE name = :name", array(':name' => $this->name))->fetchField();
    $this->vid = $vid ? $vid : NULL;
  }

  $transaction = db_transaction();

  try {
    // If we have no vid or our vid is a string, this is a new view.
    if (!empty($this->vid)) {
      // remove existing table entries
      foreach ($this->db_objects() as $key) {
        db_delete('views_' . $key)
          ->condition('vid', $this->vid)
          ->execute();
      }
    }

    $this->save_row(!empty($this->vid) ? 'vid' : FALSE);

    // Save all of our subtables.
    foreach ($this->db_objects() as $key) {
      $this->_save_rows($key);
    }
  }
  catch (Exception $e) {
    $transaction->rollback();
    watchdog_exception('views', $e);
    throw $e;
  }

  $this->save_locale_strings();

  // Clear caches.
  views_invalidate_cache();
}