function 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 1882

Class

view
An object to contain all of the data to generate a view.

Code

public function save() {
    if ($this->vid == 'new') {
        $this->vid = NULL;
    }
    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;
    }
    // Let modules modify the view just prior to saving it.
    module_invoke_all('views_view_presave', $this);
    $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 the relevant caches.
    cache_clear_all('views_block_items:', 'cache_views', TRUE);
    views_invalidate_cache('ctools_export:views_view:' . $this->name);
    // Notify modules that this view has been saved.
    module_invoke_all('views_view_save', $this);
}