[Dbix-class] A DBIC based library

Ben. B. bennymack at gmail.com
Tue Jun 23 18:40:47 GMT 2009


I'm not a DBIC support expert but I do have some experience trying to
sub-class result classes and I think what you need to do is make a result
source for this sub-class so that it is returned when you do:
$schema->resultset('Comment');

This is an adaptation of what is in DBIx::Class::Manual::Cookbook "Arbitrary
SQL through a custom ResultSource". The key components are that I cloned the
result source of the module to be sub-classed, set its result_class to the
sub-class, then registered it with the schema. Feel free to respond with a
more correct answer but I figured this had gone a while without a reply so
I'd give it a shot...

package PiraciDrogowi::DBSchema;
use strict;
use warnings;
use base qw(DBIx::Class::Schema);

#__PACKAGE__->load_namespaces();
__PACKAGE__->load_classes( {
    'CatalystX::Comments' =3D> [ 'DBICResult', ],
    'PiraciDrogowi::DBSchema' =3D> [ 'Comment',  'Incydent', ],
}, );

my $source =3D CatalystX::Comments::DBICResult->result_source_instance();
my $new_source =3D $source->new( $source );
# $new_source->source_name( 'Comment' ); # Not sure if this step is
necessary.
$new_source->result_class( 'PiraciDrogowi::DBSchema::Comment' );

__PACKAGE__->register_extra_source( 'Comment' =3D> $new_source );

1;


package PiraciDrogowi::DBSchema::Incydent;
use strict;
use warnings;
use base 'DBIx::Class';

__PACKAGE__->load_components(qw/TimeStamp PK::Auto Core/);
__PACKAGE__->table('incydent');
__PACKAGE__->add_columns(
    id =3D>  { data_type =3D> 'integer', is_auto_increment =3D> 1, is_nulla=
ble =3D>
0 },
    item_id  =3D>  { data_type =3D> 'integer', is_nullable =3D> 0 },
);

__PACKAGE__->set_primary_key('id');

1;

######## test.pl
use strict;
use warnings;
use lib 'lib';

use PiraciDrogowi::DBSchema;

my $schema =3D PiraciDrogowi::DBSchema->connect( 'dbi:SQLite:dbname=3Dpirac=
i.db'
);
my $rs =3D $schema->resultset( 'Comment' );
printf "count =3D %d\n", $rs->count;
my $comment =3D $rs->first;
printf "id =3D %d\n", $comment->incydent->id;


$ dbicdeploy -Ilib  PiraciDrogowi::DBSchema 'dbi:SQLite:dbname=3Dpiraci.db'
# After inserting some test rows
$ perl bin/test.pl
count =3D 1
id =3D 1


On Sat, Jun 6, 2009 at 5:27 AM, Zbigniew Lukasiak <zzbbyy at gmail.com> wrote:

> 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 =3D>  { data_type =3D> 'integer', is_auto_increment =3D> 1, is_nullab=
le =3D> 0
> },
>  item_id  =3D>  { data_type =3D> 'integer', is_nullable =3D> 0 },
>  body =3D> { data_type =3D> 'text', default_value =3D> undef, is_nullable=
 =3D> 1 },
>  nick =3D> { data_type =3D> 'varchar', default_value =3D> undef, is_nulla=
ble =3D> 1
> },
>  email =3D> { data_type =3D> 'varchar', default_value =3D> undef, is_null=
able =3D>
> 1 },
>  web_site =3D> { data_type =3D> 'varchar', default_value =3D> undef,
> is_nullable =3D> 1 },
>  ip =3D> { data_type =3D> 'varchar', default_value =3D> undef, is_nullabl=
e =3D> 1
> },
>  t_created =3D> { data_type =3D> 'datetime', set_on_create =3D> 1 },
>  t_updated =3D> { data_type =3D> 'datetime', set_on_create =3D> 1,
> set_on_update =3D> 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 =3D> '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 =3D PiraciDrogowi::DBSchema->connect(
> 'dbi:SQLite:dbname=3Dpiraci.db' );
> my $rs =3D $schema->resultset('Comment');
> print $rs->count, "\n";
> my $comment =3D $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/
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20090623/4d9=
6562f/attachment.htm


More information about the DBIx-Class mailing list