[Dbix-class] Subclass a Result class.

Rob Kinyon rob.kinyon at gmail.com
Tue Apr 27 13:28:18 GMT 2010


On Tue, Apr 27, 2010 at 00:46, Bill Moseley <moseley at hank.org> wrote:
> 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 => '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 => 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.

sub pending_tracks {
    my $self = shift;
    my $rs = $self->tracks({ pending => 1 });
    $rs->result_class( 'My::App::Track::Pending' );
    return $rs;
}

Rob



More information about the DBIx-Class mailing list