Fork me on GitHub

Controller Attributes

To always execute one or more methods before executing the action of the controller, set them in $before.

For example, here's how to always run set_venues() before every action in EventsController:

<?php
class EventsController extends MvcPublicController {

  var $before = array('set_venues');
  
  public function set_venues() {
    $this->load_model('Venue');
    $venues = $this->Venue->find();
    $this->set('venues', $venues);
  }
  
}
?>

To always execute one or more methods after executing the action of the controller, set them in $after.

For example, here's how to always run set_venues() after every action in EventsController:

<?php
class EventsController extends MvcPublicController {

  var $after = array('set_venues');
  
  public function set_venues() {
    $this->load_model('Venue');
    $venues = $this->Venue->find();
    $this->set('venues', $venues);
  }
  
}
?>
View alone

default_searchable_fields

This can be set to determine what fields are used in searches within the administrative interface (in an admin controller) or in the default search functionality of a public controller.

<?php
class AdminVenuesController extends MvcAdminController {

  var $default_columns = array(
    'id',
    'name',
    'url' => 'URL'
  );
  var $default_searchable_fields = array('name', 'url');

}
?>

Including fields from associated models

To search fields from associated models, you'll need to set list those models in default_search_joins.

<?php
// Event belongs_to Venue and has_and_belongs_to_many Speakers

class Event extends MvcModel {

  var $belongs_to = array('Venue');
  var $has_and_belongs_to_many = array(
    'Speaker' => array(
      'join_table' => '{prefix}events_speakers',
      'fields' => array('id', 'first_name', 'last_name')
    )
  );

}

// We can then set $default_search_joins and $default_searchable_fields to include Speaker and Venue

class AdminEventsController extends MvcAdminController {

  var $default_search_joins = array('Speaker', 'Venue');
  var $default_searchable_fields = array('Speaker.first_name', 'Speaker.last_name', 'Venue.name');
  var $default_columns = array(
    'id',
    'date' => array('value_method' => 'admin_column_date'),
    'time' => array('value_method' => 'admin_column_time'),
    'venue' => array('value_method' => 'venue_edit_link'),
    'speaker_names' => 'Speakers'
  );

}
?>
View alone

default_search_joins

This can be set to make searches in the administrative interface or in the default public search include the specified associations as joins. This is helpful if you'd like to include fields from associations in default_searchable_fields.

<?php
// Event belongs_to Venue and has_and_belongs_to_many Speakers

class Event extends MvcModel {

  var $belongs_to = array('Venue');
  var $has_and_belongs_to_many = array(
    'Speaker' => array(
      'join_table' => '{prefix}events_speakers',
      'fields' => array('id', 'first_name', 'last_name')
    )
  );

}

// We can then set $default_search_joins and $default_searchable_fields to include Speaker and Venue

class AdminEventsController extends MvcAdminController {

  var $default_search_joins = array('Speaker', 'Venue');
  var $default_searchable_fields = array('Speaker.first_name', 'Speaker.last_name', 'Venue.name');
  var $default_columns = array(
    'id',
    'date' => array('value_method' => 'admin_column_date'),
    'time' => array('value_method' => 'admin_column_time'),
    'venue' => array('value_method' => 'venue_edit_link'),
    'speaker_names' => 'Speakers'
  );

}
?>
View alone

default_columns

This defines what columns are shown in an admin controller's default index view. (A generic index view is provided by WP MVC's core, but it can be overwritten by creating an index.php view in views/admin/[models], where [models] is the tableized model name (e.g. venues)).

Each value can be one of the following:

  1. String (without an associative key): Table cell values are found by calling this property on the object. The table header is the string, titleized.
  2. String (with an associative key): The key is used for the property, and the value is used for the table header.
  3. Array (with an associative key): The key is used for the property. A label key in the array can be used to set the table header, and a value_method key can be used to signify that the returned value of the specified method will be used for the table cell.
<?php
class AdminEventsController extends MvcAdminController {
  
  var $default_search_joins = array('Speaker', 'Venue');
  var $default_searchable_fields = array('Speaker.first_name', 'Speaker.last_name', 'Venue.name');
  var $default_columns = array(
    'id',
    'date' => array('value_method' => 'admin_column_date'),
    'time' => array('value_method' => 'admin_column_time'),
    'venue' => array('value_method' => 'venue_edit_link'),
    'speaker_names' => 'Speakers'
  );
  
  public function admin_column_date($object) {
    return empty($object->date) ? null : date('F jS, Y', strtotime($object->date));
  }
  
  public function admin_column_time($object) {
    return empty($object->time) ? null : date('g:ia', strtotime($object->time));
  }
  
  public function venue_edit_link($object) {
    return empty($object->venue) ? null : HtmlHelper::admin_object_link($object->venue, array('action' => 'edit'));
  }
  
}
?>