14. use MT::Blog;
use MT::Website;
my $website_id = 1;
my $website =
MT::Website->load($website_id)
or die;
my $blog = new MT::Blog;
$website->add_blog($blog);
my $blogs = $website->blogs;
18. • The website uses same table as the
blog.
• The class of the website is “website”,
also the class of the blog is “blog”.
• The blog has parent_id that means the
belonging website.
19. mysql> select blog_id, blog_class, blog_parent_id
from mt_blog;
+---------+------------+----------------+
| blog_id | blog_class | blog_parent_id |
+---------+------------+----------------+
| 1 | website | NULL |
| 2 | blog | 1 |
| 3 | blog | 1 |
| 4 | blog | 1 |
| 5 | website | NULL |
+---------+------------+----------------+
5 rows in set (0.01 sec)
35. You must override
ʻpack_revisionʼ and
ʻunpack_revisionʼ
method on your object.
36. sub pack_revision {
my $obj = shift;
my $values = MT::Revisable::pack_revision( $obj );
# add category placements and tag associations
my ( @tags, @cats );
if ( my $tags = $obj->get_tag_objects ) {
@tags = map { $_->id } @$tags
if @$tags;
}
# a revision may remove all the tags
$values->{__rev_tags} = @tags;
my $primary = $obj->category;
if ( my $cats = $obj->categories ) {
@cats = map { [
$_->id,
$_->id == $primary->id ? 1 : 0
] } @$cats
if @$cats;
}
# a revision may remove all the categories
$values->{__rev_cats} = @cats;
$values;
}
37. }
sub unpack_revision { if ( my $rev_cats = delete $packed_obj-
my $obj = shift; >{__rev_cats} ) {
my ($packed_obj) = @_; $obj->clear_cache('category');
MT::Revisable::unpack_revision( $obj, $obj->clear_cache('categories');
@_ );
my ( $cat, @cats );
# restore category placements and tag if ( @$rev_cats ) {
associations my ($primary) = grep { $_->[1] }
if ( my $rev_tags = delete $packed_obj- @$rev_cats;
>{__rev_tags} ) { $cat = MT::Category-
delete $obj->{__tags}; >lookup( $primary->[0] );
delete $obj->{__tag_objects}; my $cats = MT::Category-
MT::Tag->clear_cache(datasource => >lookup_multi([ map { $_->[0] } @
$obj->datasource, $rev_cats ]);
($obj->blog_id ? (blog_id => my @cats = sort { $a->label cmp
$obj->blog_id) : ())); $b->label } grep { defined } @$cats;
$obj->{__missing_cats_rev} = 1
require MT::Memcached; if scalar( @cats ) !=
MT::Memcached->instance- scalar( @$cats );
>delete( $obj->tag_cache_key ); }
$obj->cache_property( 'category',
if ( @$rev_tags ) { undef, $cat );
my $lookups = MT::Tag- $obj->cache_property( 'categories',
>lookup_multi($rev_tags); undef, @cats );
my @tags = grep { defined } @ }
$lookups; }
$obj->{__tags} = [ map { $_-
>name } @tags ];
$obj->{__tag_objects} = @tags;
$obj->{__missing_tags_rev} = 1
if scalar( @tags ) !=
scalar( @$lookups );
}
else {
$obj->{__tags} = [];
$obj->{__tag_objects} = [];
}