-
Notifications
You must be signed in to change notification settings - Fork 9
English plugin dev 4 1
semuel edited this page Mar 27, 2012
·
4 revisions
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.
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
Lets say that you want to create MT::Foo
- 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
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
- 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
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;
-
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’,
- Specify each column
-
indexes => {},- Three indexes are defined for the database table
- blog_id => 1,
- created_on => 1,
- modified_on => 1,
- Three indexes are defined for the database table
-
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
| 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 |
| not null | Do no allow NULL values. (recommanded) |
| auto_increment | Integer that is assigned automatically to new objects |
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)
$MT_DIR/
|__ plugins/
|__ MyPlugin11/
|__ config.yaml
|__ lib/
|_ MyPlugin11/
| |__ L10N.pm
| |_ L10N/
| |_ en_us.pm
| |_ ja.pm
|_ MT
|__ Foo.pm
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();
Prev:MT Objects and Database << Index >> Next:Creating a new application