Resurrecting this thread, because I have also been struggling with this of late, and I thought it would be a good idea to post what I have learnt.
I found that to make things work, I needed to put the table classes before the main schema class, in the source file, and that I can't have any extra levels in the class hierarchy between the Schema class and the table classes. (Not sure why)
Here is an example script that I got working
#!/usr/bin/perl
# Test/Demo of a single file perl script using DBIx::Class.
# Notes on how to make this work:
# The Table class(es) need to appear before the Schema class or
# the rest of the program.
# The package of the table classes can't have any extra levels
# (eg <schema>::Result::<table> won't work.
# Remember the deploy command if the initial DB is empty.
# Put the DBIx::Class packages in the same file
package LocalTest::Schema::TestTable;
use base 'DBIx::Class::Core';
use DateTime;
__PACKAGE__->table('tblTest');
__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
__PACKAGE__->add_columns(
'id' => { data_type=>'int', is_auto_increment=>1 },
'date' => { data_type=>'datetime' },
'text' => { data_type=>'varchar', size=>20, is_nullable=>1 },
);
__PACKAGE__->set_primary_key('id');
sub new
{
my( $class, $arg ) = @_;
$arg->{date} ||= DateTime->now;
return $class->next::method( $arg );
}
package LocalTest::Schema;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_namespaces;
__PACKAGE__->load_classes('TestTable');
# Back to the main program
package main;
use strict;
use warnings;
use diagnostics;
use English;
use DateTime;
# use LocalTest::Schema; # This import does not work, & not needed.
my $schema = LocalTest::Schema->connect('dbi:SQLite:dbname=:memory:');
$schema->deploy();
my $row1 = $schema->resultset('TestTable')->create({'text'=>'foo_bar'}
+);
my $row2 = $schema->resultset('TestTable')->create({});
my $row3 = $schema->resultset('TestTable')->create({'text'=>'frodo'});
my $row_rs = $schema->resultset('TestTable')->search({'id'=>[1,2]});
my @rows = $row_rs->all;
printf "Found %d rows\n", $row_rs->count;
|