Fork me on GitHub

Saving Data

To create a record in the database for a model, call create(). create() is most commonly used with data submitted from a form that was made using FormHelper:

<?php
class AdminVenuesController extends AdminMvcController {

  public function add() {
    if (!empty($this->params['data']) && !empty($this->params['data']['Venue'])) {
      $object = $this->params['data']['Venue'];
      if (empty($object['id'])) {
        $this->Venue->create($this->params['data']);
        $id = $this->Venue->insert_id;
        $url = MvcRouter::admin_url(array('controller' => $this->name, 'action' => 'edit', 'id' => $id));
        $this->flash('notice', 'Successfully created!');
        $this->redirect($url);
      }
    }
    $this->set_object();
  }

}
?>

Please note that the workflow above can be handled behind the scenes using create_or_save().

To save a record in the database for a model, call save(). save() will check for the presence of the model's primary key; if it is present, it will update the record with that ID; if it isn't present, it will create a new record. save is most commonly used with data submitted from a form that was made using FormHelper:

<?php
class AdminVenuesController extends AdminMvcController {

  public function edit() {
    if (!empty($this->params['data']) && !empty($this->params['data']['Venue'])) {
      $object = $this->params['data']['Venue'];
      if ($this->Venue->save($this->params['data'])) {
        $this->flash('notice', 'Successfully saved!');
        $this->refresh();
      } else {
        $this->flash('error', $this->Venue->validation_error_html);
      }
    }
    $this->set_object();
  }

}
?>

Please note that the workflow above can be handled behind the scenes using create_or_save().

To update one or more fields of a record, use update(). The first argument should be the ID of the record, and the second argument should be an array of the data that will be updated.

<?php

class Venue extends MvcModel {
  
  public function after_save($object) {
    $this->update_sort_name($object);
  }
  
  public function update_sort_name($object) {
    $sort_name = $object->name;
    $article = 'The';
    $article_ = $article.' ';
    if (strcasecmp(substr($sort_name, 0, strlen($article_)), $article_) == 0) {
      $sort_name = substr($sort_name, strlen($article_)).', '.$article;
    }
    $this->update($object->__id, array('sort_name' => $sort_name));
  }
  
}

?>
View alone

update_all

To update one or more fields of multiple records, use update_all(). The first argument should be an array of the data that will be updated, and the second argument, which is optional, can be an array of options. The commonly used option would be conditions, which would make the update only affect records that match the specified conditions.

<?php

class Event extends MvcModel {
  
  public function make_past_events_not_public() {
    $this->update_all(
      array('is_public' => 0),
      array('date <' => date('Y-m-d'))
    );
  }
  
}

?>

Ideally, create() would be used to create records, but if you need to directly insert data, insert() can be used. insert() behaves almost identically to $wpdb->insert(), but it takes only one argument, the array of data that will be inserted into the model's table.