[Dbix-class] Subclass a Result class.

Matt S Trout mst at shadowcat.co.uk
Wed Apr 28 14:27:22 GMT 2010


On Tue, Apr 27, 2010 at 12:59:05PM -0700, Bill Moseley wrote:
> On Tue, Apr 27, 2010 at 6:28 AM, Rob Kinyon <rob.kinyon at gmail.com> wrote:
> 
> >
> > sub pending_tracks {
> >    my $self = shift;
> >    my $rs = $self->tracks({ pending => 1 });
> >    $rs->result_class( 'My::App::Track::Pending' );
> >    return $rs;
> > }
> >
> 
> Thanks, I think I was confused by Helper::Row::SubClass  I'm curious how
> that differs from the inflate_result example in the Cookbook?   When would
> one be used over the other?

You use inflate_result because the data being fed to object construction
determines the class.

So if you overrode inflate_result then $rs->tracks->all would return a mixture
of tracks and pending tracks.

Setting $rs->result_class only affects the rs you set it on.

Calling $rs->search({}, { result_class => $class }) affects the -returned-
rs and all descendants thereof.

Use the option that provides the desired semantic for your codebase.
 
> Now, a related question.  The FAQ describes how to store non-database data
> in the class.  What I'm not clear on is how to set the data per instance
> when using the above method.
> 
> A different example might help explain:
> 
> For example, say I have a database for managing conferences.  At a
> conference Attendees sign up for one or more Sessions as a typical
> many-to-many relationship.
> 
> I can get Attendees for a given session:  my $session_attendees =
> $session->attendees;
> For a give attendee (found via a Session) I'd like that Attendee object to
> "know" what session it came from.
> 
> Say another many-to-many relationship exists -- each Attendee writes a
> review of each Session they attend.
> 
> In other words,
> 
> $schema->resultset( 'Attendee' )->first->reviews;  # returns reviews for ALL
> sessions attended by the first attendee.
> 
> But,
> 
> $session_attendee = $session->attendees->first;  # First Attendee of a
> specific Session.
> 
> $session_attendee->reviews;  # return only review for that specific session
> by this attendee.
> 
> For that to happen the $session_attendee needs to be specific to a given
> session (Session id is stored with the $session_attendee instance).

You just answered your own question.

You want a result from the many-many link table and to traverse from there,
since that table has (session_id, attendee_id).

-- 
        Matt S Trout         Catalyst and DBIx::Class consultancy with a clue
     Technical Director      and a commit bit: http://shadowcat.co.uk/catalyst/
 Shadowcat Systems Limited
  mst (@) shadowcat.co.uk        http://shadowcat.co.uk/blog/matt-s-trout/



More information about the DBIx-Class mailing list