9 my $schema = DBICTest->init_schema();
12 # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch)
13 # only the requested me.name column will be fetched.
15 # reference sql with select => [...]
16 # SELECT me.name, cds.title, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ...
18 my $rs = $schema->resultset('Artist')->search(
19 { 'cds.title' => { '!=', 'Generic Manufactured Singles' } },
21 prefetch => [ qw/ cds / ],
22 order_by => [ { -desc => 'me.name' }, 'cds.title' ],
23 select => [qw/ me.name cds.title / ],
27 is ($rs->count, 2, 'Correct number of collapsed artists');
28 my $we_are_goth = $rs->first;
29 is ($we_are_goth->name, 'We Are Goth', 'Correct first artist');
30 is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist');
31 is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist');
32 }, 'explicit prefetch on a keyless object works');
37 my $rs = $schema->resultset('CD')->search(
40 order_by => [ { -desc => 'me.year' } ],
43 my $years = [qw/ 2001 2001 1999 1998 1997/];
46 [ $rs->search->get_column('me.year')->all ],
48 'Expected years (at least one duplicate)',
52 for my $cd ($rs->all) {
53 my $data->{year} = $cd->year;
54 for my $tr ($cd->tracks->all) {
55 push @{$data->{tracks}}, { $tr->get_columns };
57 push @cds_and_tracks, $data;
60 my $pref_rs = $rs->search ({}, { columns => ['year'], prefetch => 'tracks' });
62 my @pref_cds_and_tracks;
63 for my $cd ($pref_rs->all) {
64 my $data = { $cd->get_columns };
65 for my $tr ($cd->tracks->all) {
66 push @{$data->{tracks}}, { $tr->get_columns };
68 push @pref_cds_and_tracks, $data;
72 \@pref_cds_and_tracks,
74 'Correct collapsing on non-unique primary object'
78 [ $pref_rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all ],
80 'Correct HRI collapsing on non-unique primary object'
83 }, 'weird collapse lives');
87 # test implicit prefetch as well
89 my $rs = $schema->resultset('CD')->search(
90 { title => 'Generic Manufactured Singles' },
93 select => [qw/ me.title artist.name / ],
98 is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
99 isa_ok ($cd->artist, 'DBICTest::Artist');
100 is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
102 }, 'implicit keyless prefetch works');
107 $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
109 qr|\QCan't inflate manual prefetch into non-existent relationship 'artist' from 'Track', check the inflation specification (columns/as) ending in 'artist.name'|,
110 'Sensible error message on mis-specified "as"',