[Dbix-class] InflateColumn::DateTime, inheritance, and insert/delete problems

Peter Rabbitson rabbit+dbic at rabbit.us
Wed Mar 11 06:38:52 GMT 2009


Steve Caldwell wrote:
> Consider the following script:
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> # mysql>
> #   create database foo;
> #   create table foo.tblfoo (
> #     fooid integer not null auto_increment,
> #     mydate datetime,
> #     primary key (fooid)
> #   );
> #   grant all privileges on foo.* to foouser at localhost
> #     identified by 'foopassword';
> 
> use DateTime;
> 
> {
>     package My::Plugin::RowStuff;
>     sub insert {
>         my $self = shift;
>         warn "I'm going to do something before inserting";
>         $self->next::method(@_);
>     }
>     sub delete {
>         my $self = shift;
>         warn "I'm going to do something before deleting";
>         $self->next::method(@_);
>     }
>     1;
> 
>     package My::SchemaBase;
>     use base 'DBIx::Class';
>     __PACKAGE__->load_components(qw/
>         InflateColumn::DateTime
>         +My::Plugin::RowStuff
>         Core
>     /);
>     1;
> 
>     package My::Schema::Foo;
>     use base 'My::SchemaBase';
>     __PACKAGE__->table('tblfoo');
>     __PACKAGE__->add_columns(
>         fooid  => {},
>         mydate => { data_type => 'datetime' },
>     );
>     __PACKAGE__->set_primary_key('fooid');
>     1;
> 
>     package My::Schema;
>     use base 'DBIx::Class::Schema';
>     My::Schema->load_classes(qw/Foo/);
>     1;
> }
> 
> my $schema = My::Schema->connect(
>     'DBI:mysql:database=foo', 'foouser', 'foopassword'
> );
> 
> my $obj = $schema->resultset('Foo')->create({
>     mydate => DateTime->now(),
> });
> 
> $obj->delete();
> 
> exit 0;
> 
> 1;
> 
> 
> When I run this, my custom insert and delete methods in
> My::Plugin::RowStuff do not get run.  I can't figure out why not, but I
> have identified the following:
> 
> 1) removing InflateColumn::DateTime will make it work
> 
> 2) removing the inheritance of My::SchemaBase will work - i.e. have
> My::Schema::Foo inherit directly from DBIx::Class and call
> load_components(...) in that package.
> 
> 3) adding skeleton insert and delete methods to My::SchemaBase will
> work, so that it now looks like:
> 
>     package My::SchemaBase;
>     use base 'DBIx::Class';
>     __PACKAGE__->load_components(qw/
>         InflateColumn::DateTime
>         +My::Plugin::RowStuff
>         Core
>     /);
>     sub insert { shift->next::method(@_) }
>     sub delete { shift->next::method(@_) }
>     1;
> 
> As #1 and #2 aren't viable for my codebase, I'm going with the odd #3
> solution.  Does anyone know why InflateColumn::DateTime is exhibiting
> this behavior?
> 

For now I'd recommend you solution 4 - change:

     __PACKAGE__->load_components(qw/
         InflateColumn::DateTime
         +My::Plugin::RowStuff
         Core
to
     __PACKAGE__->load_components(qw/
         +My::Plugin::RowStuff
         InflateColumn::DateTime
         Core

In related news this seems to be a weird misdesign of IC. I'm testing
for it now...



More information about the DBIx-Class mailing list