English plugin dev 4 1 - Hiranyaloka/Documentation GitHub Wiki

Creating Your Own Objects

Introduction

In the previous chapter we saw how to play with existing MT objects. But sometimes you need your own object type, to save plugin-related data.

My very own objects?

So here are the rules: the object need to subclass MT::Object, or an existing MT object. (i.e. MT::Entry)

And you need to register it in the plugin config.yaml

So… how do I do that?

Lets say that you want to create MT::Foo

MT::Foo Specification

  • Inherit from MT::Object
  • Perl Class MT::Foo
  • Type name foo
  • Columns
    • id : Object ID
    • blog_id : Blog ID (the blog that this object is belong to)
    • title : title, up to 255 characters
    • bar : Text information
  • Indexes on:
    • blog_id
    • created_on
    • modified_on
  • Data source : mt_foo (the table name in the DB)
  • Primary key : id
  • Audit data: automatically add creating and modification timestamp

config.yaml

id: MyPlugin11
key: MyPlugin11
name: <__trans phrase="Sample Plugin Uniquely Object">
version: 1.0
description: <__trans phrase="_PLUGIN_DESCRIPTION">
author_name: <__trans phrase="_PLUGIN_AUTHOR">
author_link: http://www.example.com/about/
doc_link: http://www.example.com/docs/
l10n_class: MyPlugin11::L10N

schema_version: 0.01
object_types:
    foo: MT::Foo

Commentary

  • schema_version: 0.01
    • The version of the database schema
    • Increasing this number will cause MT to re-install the plugin
  • object_types
    • Declaring new object types in the system
  • foo: MT::Foo
    • Type name : foo
    • Perl Class : MT::Foo

MT/Foo.pm

package MT::Foo;
use strict;

use base qw( MT::Object );

__PACKAGE__->install_properties ({
    column_defs => {
        'id'      => 'integer not null auto_increment',
        'blog_id' => 'integer not null',
        'title'   => 'string(255) not null',
        'bar'     => 'text',
    },
    indexes => {
        blog_id => 1,
        created_on => 1,
        modified_on => 1,
    },
    child_of => 'MT::Blog',
    audit => 1,
    datasource  => 'foo',
    primary_key => 'id',
    class_type  => 'foo',
});

1;

Commentary

  • package MT::Foo;
    • Perl package name: MT::Foo
  • use base qw( MT::Object );
    • Inheriting from MT::Object
  • __PACKAGE__->install_properties ({});
    • This is where the object information goes
  • column_defs => {},
    • Specify each column
      • ‘id’ => ‘integer not null auto_increment’,
      • ‘blog_id’ => ‘integer not null’,
      • ‘title’ => ‘string(255) not null’,
      • ‘bar’ => ‘text’,
  • indexes => {},
    • Three indexes are defined for the database table
      • blog_id => 1,
      • created_on => 1,
      • modified_on => 1,
  • audit => 1,
    • Automatically add creation and modification timestamps
    • created_on, created_by, modified_on, modified_by columns。
  • datasource => 'foo',
    • The name of the database table will be mt_foo
  • primary_key => 'id',
    • The primary key will be ‘id’
  • class_type => 'foo',
    • And the type-name (as in MT→model(‘xxx’)) of this object will be foo

Column types and options

Column types

string String. please specify the string size, i.e. string(255)
text
boolean
smallint 16bit integer
integer 32bit integer
float
blob Binary data
datetime
timestamp Same as datetime, but will update automatically when you save the object

Column options

not null Do no allow NULL values. (recommanded)
auto_increment Integer that is assigned automatically to new objects

The resulted table

Lets check that the table was created as expected

mysql> show create table mt_foo \G
*************************** 1. row ***************************
       Table: mt_foo
Create Table: CREATE TABLE `mt_foo` (
  `foo_id` int(11) NOT NULL auto_increment,
  `foo_bar` mediumtext,
  `foo_blog_id` int(11) NOT NULL,
  `foo_class` varchar(255) default 'foo',
  `foo_created_by` int(11) default NULL,
  `foo_created_on` datetime default NULL,
  `foo_modified_by` int(11) default NULL,
  `foo_modified_on` datetime default NULL,
  `foo_title` varchar(255) NOT NULL,
  PRIMARY KEY  (`foo_id`),
  KEY `mt_foo_modified_on` (`foo_modified_on`),
  KEY `mt_foo_created_on` (`foo_created_on`),
  KEY `mt_foo_class` (`foo_class`),
  KEY `mt_foo_blog_id` (`foo_blog_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

Directory Structure

$MT_DIR/
|__ plugins/
   |__ MyPlugin11/
      |__ config.yaml
      |__ lib/
         |_ MyPlugin11/
         |  |__ L10N.pm
         |  |_ L10N/
         |     |_ en_us.pm
         |     |_ ja.pm
         |_ MT
            |__ Foo.pm

How to use MT::Foo

OK, we defined and coded this object type. Using it is identical to MT’s integral objects

my $class_name = 'foo';
my $class = MT->model($class_name);
my $foo = $class->new();
$foo->blog_id(2);
$foo->title('MT::Foo test');
$foo->bar('Hello, world!!');
$foo->save()
    or die $foo->errstr();

my $title = $foo->title();

Plugin Download

MyPlugin11.zip(2.38KB)

Navigation

Prev:MT Objects and Database << Index >> Next:Creating a new application

⚠️ **GitHub.com Fallback** ⚠️