[Dbix-class] overloading new() or insert(), a problem when using UUIDColumns

Hu Hailin i at h2l.name
Thu Apr 7 09:15:52 GMT 2011


Hi,

I got a weird problem today. My classes are something like this:

a base class for other resultsets
----
package MyApp::Schema::Result;

use strict;
use warnings;

use base 'DBIx::Class::Core';

__PACKAGE__->load_components(qw/InflateColumn::DateTime UUIDColumns/);
=cut
sub new {
  my ( $class, $attrs ) = @_;

  my $column = 'create_time';
  if ( $class->has_column( $column ) and not defined $attrs->{ $column } ) {
    $attrs->{ $column } = \'CURRENT_TIMESTAMP';
  }

  return $class->next::method( $attrs );
}
=cut
sub insert {
  my $self = shift;

  my $create_time = 'create_time';
  unless ( defined $self->get_column( $create_time ) ) {
    $self->store_column( $create_time, \'CURRENT_TIMESTAMP' )
  }

  $self->next::method(@_);
}

1;
----
a resultset module
----
package MyApp::Schema::Result::Foo;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use Moose;
use MooseX::NonMoose;
use namespace::autoclean;
extends 'MyApp::Schema::Result';

__PACKAGE__->table("foo");
__PACKAGE__->add_columns(
  "foo_id",
  { data_type => "char", is_nullable => 0, size => 36 },
...
 "create_time",
  {
    data_type     => "timestamp",
    default_value => "2010-01-01 00:00:00",
    is_nullable   => 0,
  },
__PACKAGE__->set_primary_key("foo_id");


# Created by DBIx::Class::Schema::Loader v0.07002 @ 2011-03-01 11:34:39
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:sa/CilRe9R2mjbCCJJWBzQ


__PACKAGE__->uuid_columns('achieve_id');

__PACKAGE__->meta->make_immutable;
1;
----

As you can see, I try to set a default value for create_time column
and overload new() method according to dbic's cookbook.
It runs all right, but when I wrote a test script outside Catalyst,
UUIDColumns didn't work. I use the same schema class both inside and
outside Catalyst.
I have no idea but read UUIDColumns source code, then tried to
overload insert() method. It works!

My question, does it mean overloading insert() is preferred, at least
for setting default value for a column?
And I'll be very very happy if somebody can tell me why it works under
Catalyst env.

Sorry for that I can't give out a complete runable code sample...
-- 
islue



More information about the DBIx-Class mailing list