Not a developer? Go to MovableType.com

Documentation

CREATING A SUBCLASS

Creating a subclass of MT::Object is very simple; you simply need to define the properties and metadata about the object you are creating. Start by declaring your class, and inheriting from MT::Object:

package MT::Foo;
use strict;

use base 'MT::Object';

__PACKAGE__->install_properties($args)

Then call the install_properties method on your class name; an easy way to get your class name is to use the special __PACKAGE__ variable:

__PACKAGE__->install_properties({
    column_defs => {
        'id' => 'integer not null auto_increment',
        'foo' => 'string(255)',
    },
    indexes => {
        foo => 1,
    },
    primary_key => 'id',
    datasource => 'foo',
});

installproperties_ performs the necessary magic to install the metadata about your new class in the MT system. The method takes one argument, a hash reference containing the metadata about your class. That hash reference can have the following keys:

column_defs

The definition of the columns (fields) in your object. Column names are also used for method names for your object, so your column name should not contain any strange characters. (It could also be used as part of the name of the column in a relational database table, so that is another reason to keep column names somewhat sane.)

The value for the columns key should be a reference to an hashref containing the key/value pairs that are names of your columns matched with their schema definition.

The type declaration of a column is pseudo-SQL. The data types loosely match SQL types, but are vendor-neutral, and each MT::ObjectDriver will map these to appropriate types for the database it services. The format of a column type is as follows:

‘column_name’ => ‘type(size) options’

The ‘type’ part of the declaration can be any one of:

  • string - For storing string data, typically up to 255 characters, but assigned a length identified by ‘(size)’.

  • integer - For storing integers, maybe limited to 32 bits.

  • boolean - For storing boolean values (numeric values of 1 or 0).

  • smallint - For storing small integers, typically limited to 16 bits.

  • datetime - For storing a full date and time value.

  • timestamp - For storing a date and time that automatically updates upon save.

  • blob - For storing binary data.

  • text - For storing text data.

  • float - For storing floating point values.

Note: The physical data storage capacity of these types will vary depending on the driver’s implementation.

The ‘(size)’ element of the declaration is only valid for the ‘string’ type.

The ‘options’ element of the declaration is not required, but is used to specify additional attributes of the column. Such as:

  • not null - Specify this option when you wish to constrain the column so that it must contain a defined value. This is only enforced by the database itself, not by the MT::ObjectDriver.

  • auto_increment - Specify for integer columns (typically the primary key) to automatically assign a value.

  • primary key - Specify for identifying the column as the primary key (only valid for a single column).

  • indexed - Identifies that this column should also be individually indexed.

  • meta - Declares the column as a meta column, which means it is stored in a separate table that is used for storing metadata. See “Metadata” for more information.

  • indexes - Specifies the column indexes on your objects.

The value for the indexes key should be a reference to a hash containing column names as keys, and the value 1 for each key - each key represents a column that should be indexed:

indexes => {
    'column_1' => 1,
    'column_2' => 1,
},

For multi-column indexes, you must declare the individual columns as the value for the index key:

indexes => {
    'column_catkey' => {
        columns => [ 'column_1', 'column_2' ],
    },
},

For declaring a unique constraint, add a ‘unique’ element to this hash:

indexes => {
    'column_catkey' => {
        columns => [ 'column_1', 'column_2' ],
        unique => 1,
    },
},
  • audit

Automatically adds bookkeeping capabilities to your class - each object will take on four new columns: created_on, created_by, modified_on, and modified_by. The created_on, created_by columns will be populated automatically (if they have not already been assigned at the time of saving the object). Your application is responsible for updating the modified_on, modified_by columns as these may require explicit application-specific assignments (i.e., your application may only want them updated during explicit user interaction with the object, as opposed to cases where the object is being changed and saved for mechanical purposes like upgrading a table).

  • datasource

The name of the datasource for your class. The datasource is a name uniquely identifying your class - it is used by the object drivers to construct table names, file names, etc. So it should not be specific to any one driver.

Please note: the length of the datasource name should be conservative; some drivers place limits on the length of table and column names.

  • meta

Specify this property if you wish to support the storage of additional metadata for this class. By doing so, a second table will be declared to MT’s registry, one that is designed to hold any metadata associated with your class.

  • class_type

If class_type is declared, an additional ‘class’ column is added to the object properties. This column is then used to differentiate between multiple object types that share the same physical table.

Note that if this is used, all searches will be constrained to match the class type of the package.

  • class_column

Defines the name of the class column (default is ‘class’) for storing classed objects (see ‘class_type’ above).

Back

Leave a Comment