[Dbix-class] Subclass a Result class.

Bill Moseley moseley at hank.org
Tue Apr 27 04:46:10 GMT 2010


I want to create separate Result classes for basically the same objects --
same columns and methods, plus a few methods specific to the new sub-class.

In my music database I have Cd objects and Track objects.  The Track objects
have a "pending" flag (for lack of a better example).

In my Cd Result class I have Cd->has_many( tracks =3D> 'App::Track' ); and,
thus, I can call $cd->tracks;

Now, say I want to have $cd->pending_tracks, which is easy:

sub pending_tracks {   shift->tracks({ pending =3D> 1 }} }

But, what if I want $cd->pending_tracks->all to return
App::Result::Track::Pending objects, where Track::Pending objects is really
just a subclass of the Track class? The reason I want to do this is so I can
have methods specific to just pending tracks.

So in the Cd class I add:

Cd->has_many( pending_tracks =3D> 'App::Track::Pending', { 'foreign.cd' =3D=
> '
self.id' }  );
sub pending_tracks { shift->pending_tracks( { pending =3D> 1 } ) }

I tried the naive approach of just subclassing Track.  Then I tried
using DBIx::Class::Helper::Row::SubClass:

package App::Result::Track::Pending;
use parent 'App::Result::Track';
__PACKAGE__->load_components(qw{Helper::Row::SubClass Core});
__PACKAGE__->subclass;
1;

But, then when I call $cd->pending_tracks I get:

DBIx::Class::Schema::throw_exception(): Namespaces
App::Result::Track::Pending and App::Result::Studio are dissimilar at
/usr/local/share/perl/5.10.0/DBIx/Class/Helpers/Util.pm line 43.

Which I assume is because there's a Track->belongs_to( studio =3D>
'App::Result::Studio' ) but in ::Studio there's a has_many to Track but not
to Track::Pending.

Can someone help out with an example of how this is suppose to be done?

Thanks,


-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20100426/f2b=
1de8b/attachment.htm


More information about the DBIx-Class mailing list