Fork me on GitHub

Model Attributes

The name of the table which the model corresponds to. A pluralized version of the model name with the current value of the WordPress database prefix$wpdb->prefix) is used by default (e.g. Venue will use a table named wp_venues).

If you set this value, you'll most likely want to include the token {prefix}at the beginning of the string, which will be replaced with the current value of $wpdb->prefix. For example:

<?php
class Venue extends MvcModel {

  var $table = '{prefix}my_venues';

}
?>
View alone

database

If you're using multiple databases under the same MySQL connection, you can specify that a model's table is in a database other than the primary WordPress database using $database. For example:

<?php
class Venue extends MvcModel {

  var $database = 'shared_database';

}
?>
View alone

includes

To include associated models by default in SELECT queries, set $includes to an array of model names for which associations have already been defined. For example:

<?php
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')
    )
  );
  var $includes = array('Venue', 'Speaker');

}
?>

This can be used to set the default order that is used in SELECT queries. For example:

<?php
class Venue extends MvcModel {

  var $order = 'Venue.name ASC, Venue.id ASC';

}
?>
View alone

display_field

The name of the field which will be used to textually represent an object in various functions that assume that such a field is available.

<?php
// models/venue.php:

class Venue extends MvcModel {

  var $display_field = 'name';

}

// views/venues/show.php:

// The following will output an anchor tag that uses $object->name for its text.
echo $this->html->venue_link($object);
?>

The display_field value will always be available in the __name property of an object ($object->__name), allowing you to represent an object textually without knowing exactly what property should be used.

Using values other than column values for display_field

Note that fields that are set in after_find() can also be used for display_field, in case the displayed value should be something other than the value of a specific column.

<?php

class Event extends MvcModel {
  
  // The events table doesn't have a column named name, but we'll
  // set $object->name in after_find() below
  var $display_field = 'name';

  public function after_find($object) {
    if (isset($object->speakers)) {
      $speaker_names = array();
      foreach($object->speakers as $speaker) {
        $speaker_names[] = $speaker->name;
      }
      $object->speaker_names = implode(', ', $speaker_names);
      $object->name = $object->speaker_names;
      if (isset($object->venue)) {
        $object->name .= ' at '.$object->venue->name;
      }
    }
  }
  
}

?>
View alone

primary_key

The name of the field which will be used as the primary key of an object. The default value is 'id'.

<?php

class Venue extends MvcModel {

  var $primary_key = 'id';

}

?>
View alone

selects

By default, all of the columns of a model's table will be retrieved in a SELECT query, but you can change this default behavior by specifying an array of columns to select in $select. For example:

<?php
class Venue extends MvcModel {

  var $selects = array('id', 'name', 'url');

}
?>
View alone

per_page

The default number of results per page that are returned when paginate() is called on the model. For example:

<?php
class Venue extends MvcModel {

  var $per_page = 8;

}
?>
View alone

hide_menu

Each model shows up as an Administration Menu by default, by you can disable this menu by setting hide_menu to false.

<?php
 
class Event extends MvcModel {
 
    var $hide_menu = false;
 
}
 
?>

For information on customizing Administration Menus, see AdminPages.