rppowell has asked for the wisdom of the Perl Monks concerning the following question:

May a knowledgeable monk assist me:

In the various tutorials I've been reading on using DBIx::Class, the classes are set up in multiple files (see http://www.17lamp.net/forum/perl/20080922220212).

I would like to have all the classes/packages in 1 file, but I get errors when I run it:

Can't locate object method "source_name" via package "DB::User" at ...

I hope to be enlightened and find a way to solve my problem;

rppowell

Replies are listed 'Best First'.
Re: DBIx::Class in a single file
by Your Mother (Archbishop) on Feb 25, 2009 at 03:50 UTC

    That seems like it should be possible. Probably not much fun to whip up an example without even a starting point though. :) Can you post the code you've tried? Using SQLite as a the DB is the easiest way to make it portable for others to test/hack.

    There are ways to have your class files autogenerated too--DBIx::Class::Schema::Loader--or just have the classes in memory. So maybe you should list your requirements before picking the code path to follow.

Re: DBIx::Class in a single file
by Anonymous Monk on Feb 25, 2009 at 13:12 UTC
Re: DBIx::Class in a single file
by rppowell (Novice) on Feb 26, 2009 at 17:51 UTC
    Hey guys; here is what I got working:
    package DB::User; use base qw(DBIx::Class); __PACKAGE__->load_components(qw(PK::Auto Core)); __PACKAGE__->table('user'); __PACKAGE__->add_columns(qw(id login password)); __PACKAGE__->set_primary_key('id'); package DB; use base qw(DBIx::Class::Schema); __PACKAGE__->load_classes(qw(User)); package main; use strict; use Data::Dumper; my $schema = DB->connect( 'DBI:mysql:database:localhost', 'username', 'password', {RaiseError => 1} ); my @users = $schema->resultset('User')->all; print Dumper($users[1]{_column_data});
    Thanks!
    rppowell
Re: DBIx::Class in a single file
by chrestomanci (Priest) on Nov 11, 2010 at 11:12 UTC

    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;

      A trick you can use to make the human read code flow more like you expect-

      # package stuff I expect/want to see first. BEGIN { # package stuff the compiler needs to see first. } package main; # my stuff that does stuff and junk. exit 0; # subs, if any __END__ =pod =cut leftover notes...