[Dbix-class] Is there a way setup a DBIC schema, result and resultset classes in one file?

Zbigniew Lukasiak zzbbyy at gmail.com
Wed Feb 25 12:23:06 GMT 2009


On Wed, Feb 25, 2009 at 12:03 PM, Peter Rabbitson <rabbit+dbic at rabbit.us> wrote:
> Zbigniew Lukasiak wrote:
>> Hi there,
>>
>> There is a question at PerlMonks about that:
>> http://perlmonks.org/?node_id=746160 and I have to add that this is
>> something I've been frequently pondering myself when sending DBIC
>> examples.
>>
>
> I don't see what the problem is. His error comes most likely from
> defining the Schema package before the Resultsource packages.

Hmm - indeed it works.  I had some vague memories that it tries to
load real files - but I've tried it once again and it worked.

I copy the example below.

Cheers,
Zbigniew

===========================================

{

    package DBSchema::User;

    use base 'DBIx::Class';

    __PACKAGE__->load_components('Core');
    __PACKAGE__->table("usr");
    __PACKAGE__->add_columns(
        "id" => {
            data_type => 'integer',
            is_auto_increment => 1,
        },
        "username" => {
            data_type => 'varchar',
            size      => '100',
        },
        "password" => {
            data_type => 'varchar',
            size      => '100',
        },
        "name" => {
            data_type => 'varchar',
            size      => '100',
          },
      );
    __PACKAGE__->set_primary_key("id");
}

{
    package DBSchema;
    use base 'DBIx::Class::Schema';

    __PACKAGE__->load_classes('User');
}

my $schema = DBSchema->connect( 'dbi:SQLite:memory' );
$schema->deploy( { add_drop_table => 1 },);
my $user_rs = $schema->resultset( 'User' );

my $user = $user_rs->create( { username => 'aaaa', password => 'aaaa',
name => 'aaaa' } );
print "User id: " . $user->id . "\n";

__OUTPUT__
User id: 1



More information about the DBIx-Class mailing list