NAME

MT::Object - Movable Type base class for database-backed objects


SYNOPSIS

Creating an MT::Object subclass:

    package MT::Foo;
    use strict;
    use MT::Object;
    @MT::Foo::ISA = qw( MT::Object );
    __PACKAGE__->install_properties({
        columns => [
            'id', 'foo',
        ],
        indexes => {
            foo => 1,
        },
        datasource => 'foo',
    });

Using an MT::Object subclass:

    use MT;
    use MT::Foo;
    ## Create an MT object to load the system configuration and
    ## initialize an object driver.
    my $mt = MT->new;
    ## Create an MT::Foo object, fill it with data, and save it;
    ## the object is saved using the object driver initialized above.
    my $foo = MT::Foo->new;
    $foo->foo('bar');
    $foo->save
        or die $foo->errstr;


DESCRIPTION

MT::Object is the base class for all Movable Type objects that will be serialized/stored to some location for later retrieval; this location could be a DBM file, a relational database, etc.

Movable Type objects know nothing about how they are stored--they know only of what types of data they consist, the names of those types of data (their columns), etc. The actual storage mechanism is in the MT::ObjectDriver class and its driver subclasses; MT::Object subclasses, on the other hand, are essentially just standard in-memory Perl objects, but with a little extra self-knowledge.

This distinction between storage and in-memory representation allows objects to be serialized to disk in many different ways--for example, an object could be stored in a MySQL database, in a DBM file, etc. Adding a new storage method is as simple as writing an object driver--a non-trivial task, to be sure, but one that will not require touching any other Movable Type code.


SUBCLASSING

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 MT::Object;
    @MT::Foo::ISA = qw( MT::Object );

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({
        columns => [
            'id', 'foo',
        ],
        indexes => {
            foo => 1,
        },
        datasource => 'foo',
    });

install_properties 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:


USAGE

System Initialization

Before using (loading, saving, removing) an MT::Object class and its objects, you must always initialize the Movable Type system. This is done with the following lines of code:

    use MT;
    my $mt = MT->new;

Constructing a new MT objects loads the system configuration from the mt.cfg configuration file, then initializes the object driver that will be used to manage serialized objects.

Creating a new object

To create a new object of an MT::Object class, use the new method:

    my $foo = MT::Foo->new;

new takes no arguments, and simply initializes a new in-memory object. In fact, you need not ever save this object to disk; it can be used as a purely in-memory object.

Setting and retrieving column values

To set the column value of an object, use the name of the column as a method name, and pass in the value for the column:

    $foo->foo('bar');

The return value of the above call will be bar, the value to which you have set the column.

To retrieve the existing value of a column, call the same method, but without an argument:

    $foo->foo

This returns the value of the foo column from the $foo object.

Saving an object

To save an object using the object driver, call the save method:

    $foo->save;

On success, save will return some true value; on failure, it will return undef, and you can retrieve the error message by calling the errstr method on the object:

    $foo->save
        or die "Saving foo failed: ", $foo->errstr;

If you are saving objects in a loop, take a look at the Note on Object Locking.

Loading an existing object or objects

You can load an object from the datastore using the load method. load is by far the most complicated method, because there are many different ways to load an object: by ID, by column value, by using a join with another type of object, etc.

In addition, you can load objects either into an array (load), or by using an iterator to step through the objects (load_iter).

load has the following general form:

    my @objects = CLASS->load(\%terms, \%arguments);

load_iter has the following general form:

    my $iter = CLASS->load(\%terms, \%arguments);

Both methods share the same parameters; the only difference is the manner in which they return the matching objects.

If you call load in scalar context, only the first row of the array @objects will be returned; this works well when you know that your load call can only ever result in one object returned--for example, when you load an object by ID.

\%terms should be either:

\%arguments should be a reference to a hash containing parameters for the search. The following parameters are allowed:

Removing an object

To remove an object from the datastore, call the remove method on an object that you have already loaded using load:

    $foo->remove;

On success, remove will return some true value; on failure, it will return undef, and you can retrieve the error message by calling the errstr method on the object:

    $foo->remove
        or die "Removing foo failed: ", $foo->errstr;

If you are removing objects in a loop, take a look at the Note on Object Locking.

Removing all of the objects of a particular class

To quickly remove all of the objects of a particular class, call the remove_all method on the class name in question:

    MT::Foo->remove_all;

On success, remove_all will return some true value; on failure, it will return undef, and you can retrieve the error message by calling the errstr method on the class name:

    MT::Foo->remove_all
        or die "Removing all foo objects failed: ", MT::Foo->errstr;

Getting the count of a number of objects

To determine how many objects meeting a particular set of conditions exist, use the count method:

    my $count = MT::Foo->count({ foo => 'bar' });

count takes the same arguments (\%terms and \%arguments) as load and load_iter, above.

Determining if an object exists in the datastore

To check an object for existence in the datastore, use the exists method:

    if ($foo->exists) {
        print "Foo $foo already exists!";
    }


NOTES

Note on object locking

When you read objects from the datastore, the object table is locked with a shared lock; when you write to the datastore, the table is locked with an exclusive lock.

Thus, note that saving or removing objects in the same loop where you are loading them from an iterator will not work--the reason is that the datastore maintains a shared lock on the object table while objects are being loaded from the iterator, and thus the attempt to gain an exclusive lock when saving or removing an object will cause deadlock.

For example, you cannot do the following:

    my $iter = MT::Foo->load_iter({ foo => 'bar ');
    while (my $foo = $iter->()) {
        $foo->remove;
    }

Instead you should do either this:

    my @foo = MT::Foo->load({ foo => 'bar' );
    for my $foo (@foo) {
        $foo->remove;
    }

or this:

    my $iter = MT::Foo->load_iter({ foo => 'bar');
    my @to_remove;
    while (my $foo = $iter->()) {
        push @to_remove, $foo
            if SOME CONDITION;
    }
    for my $foo (@to_remove) {
        $foo->remove;
    }

This last example is useful if you will not be removing every MT::Foo object where foo equals bar, because it saves memory--only the MT::Foo objects that you will be deleting are kept in memory at the same time.


AUTHOR & COPYRIGHTS

Please see the MT manpage for author, copyright, and license information.