[Dbix-class] inflate_results and source_name

Bill Moseley moseley at hank.org
Thu Apr 29 17:58:11 GMT 2010


Looking at the cookbook for inflate_result, does the source_name need to be
explicitly set somehow?
I'm a bit confused because it is getting set to one of the new classes, but
the same for all.

Added to Track class:

package MyApp::Result::Track;
...
sub inflate_result {
    my $self =3D shift;
    my $track =3D $self->next::method(@_);
    my $class =3D __PACKAGE__ . $track->position;
    $self->ensure_class_loaded( $class );
    return bless $track, $class;
}

Now:

select * from track where cd =3D 1;
 id |    name     | position | cd
----+-------------+----------+----
  1 | Track one   |        1 |  1
  2 | Track two   |        2 |  1
  3 | Track three |        3 |  1
(3 rows)

my $cd =3D $schema->resultset( 'Cd' )->find(1);

for ( $cd->tracks ) {
    printf(
        "position %d, ref=3D%s source_name=3D%s\n",
        $_->position,
        ref,
        $_->result_source->source_name,
    );
}

position 1, ref=3DMyApp::Result::Track1 source_name=3DTrack3
position 2, ref=3DMyApp::Result::Track2 source_name=3DTrack3
position 3, ref=3DMyApp::Result::Track3 source_name=3DTrack3

And sorting by -desc =3D> 'position':

position 3, ref=3DMyApp::Result::Track3 source_name=3DTrack3
position 2, ref=3DMyApp::Result::Track2 source_name=3DTrack3
position 1, ref=3DMyApp::Result::Track1 source_name=3DTrack3



$ cat Track?.pm
package MyApp::Result::Track1;
use parent MyApp::Result::Track;
__PACKAGE__->source_name('Track1');
1;
package MyApp::Result::Track2;
use parent MyApp::Result::Track;
__PACKAGE__->source_name('Track2');
1;
package MyApp::Result::Track3;
use parent MyApp::Result::Track;
__PACKAGE__->source_name('Track3');
1;




-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20100429/d05=
0dc22/attachment.htm


More information about the DBIx-Class mailing list