[Dbix-class] Subclass a Result class.

Bill Moseley moseley at hank.org
Tue Apr 27 19:59:05 GMT 2010


On Tue, Apr 27, 2010 at 6:28 AM, Rob Kinyon <rob.kinyon at gmail.com> wrote:

>
> sub pending_tracks {
>    my $self =3D shift;
>    my $rs =3D $self->tracks({ pending =3D> 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?

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 =3D
$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 =3D $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).


If I have the row objects I could see doing something like:

package Session;

...

sub attendees {
    my $self =3D shift;
    my $rs =3D $self->session_attendees;
    $rs->result_class( 'Session::Attendee' );
    my @all =3D $rs->all;
    for ( @all ) {
        $_->session_id( $self->id );
    }
    return @all;
}

But, what I'm not clear on is how to be able to return a $rs directly so
that later $rs->all sets the Session on each Session::Attendee.

Thanks,



-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20100427/5a9=
5ec39/attachment.htm


More information about the DBIx-Class mailing list