[Dbix-class] A DBIC based library

Zbigniew Lukasiak zzbbyy at gmail.com
Sat Jun 6 09:27:59 GMT 2009


Hi,

I am trying to figure out how to package a DBIC Result as a library.
You can find more background in my blog post:
http://perlalchemy.blogspot.com/2009/06/packaging-cross-cutting-catalyst.html
.   What I tried is simply defining that Result class in a separate
package - and later subclassing it:

package CatalystX::Comments::DBICResult;

use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components(qw/TimeStamp PK::Auto Core/);
__PACKAGE__->table('comment');
__PACKAGE__->add_columns(
  id =>  { data_type => 'integer', is_auto_increment => 1, is_nullable => 0 },
  item_id  =>  { data_type => 'integer', is_nullable => 0 },
  body => { data_type => 'text', default_value => undef, is_nullable => 1 },
  nick => { data_type => 'varchar', default_value => undef, is_nullable => 1 },
  email => { data_type => 'varchar', default_value => undef, is_nullable => 1 },
  web_site => { data_type => 'varchar', default_value => undef,
is_nullable => 1 },
  ip => { data_type => 'varchar', default_value => undef, is_nullable => 1 },
  t_created => { data_type => 'datetime', set_on_create => 1 },
  t_updated => { data_type => 'datetime', set_on_create => 1,
set_on_update => 1 },
);
__PACKAGE__->set_primary_key('id');

1;


And the subclass:

package PiraciDrogowi::DBSchema::Comment;

use strict;
use warnings;

use base 'CatalystX::Comments::DBICResult';

__PACKAGE__->belongs_to('incydent',
'PiraciDrogowi::DBSchema::Incydent', { id => 'item_id' });

1;


Obviously that was rather naive approach - but it was the first I thought up.
It nearly works:


use lib 'lib';

use PiraciDrogowi::DBSchema;

my $schema = PiraciDrogowi::DBSchema->connect( 'dbi:SQLite:dbname=piraci.db' );
my $rs = $schema->resultset('Comment');
print $rs->count, "\n";
my $comment = $rs->first;
print $comment->incydent->id;


This prints:
1
Can't locate object method "incydent" via package
"CatalystX::Comments::DBICResult" at a.pl line 9.

So it finds the record - but it does not see the relationships I added
in the subclass.


-- 
Zbigniew Lukasiak
http://brudnopis.blogspot.com/
http://perlalchemy.blogspot.com/



More information about the DBIx-Class mailing list