11 my $schema = DBICTest->init_schema();
14 # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch)
15 # only the requested me.name column will be fetched.
17 # reference sql with select => [...]
18 # SELECT me.name, cds.title, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ...
20 my $rs = $schema->resultset('Artist')->search(
21 { 'cds.title' => { '!=', 'Generic Manufactured Singles' } },
23 prefetch => [ qw/ cds / ],
24 order_by => [ { -desc => 'me.name' }, 'cds.title' ],
25 select => [qw/ me.name cds.title / ],
29 is ($rs->count, 2, 'Correct number of collapsed artists');
30 my $we_are_goth = $rs->first;
31 is ($we_are_goth->name, 'We Are Goth', 'Correct first artist');
32 is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist');
33 is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist');
34 }, 'explicit prefetch on a keyless object works');
39 my $rs = $schema->resultset('CD')->search(
42 order_by => [ { -desc => 'me.year' } ],
45 my $years = [qw/ 2001 2001 1999 1998 1997/];
48 [ $rs->search->get_column('me.year')->all ],
50 'Expected years (at least one duplicate)',
54 for my $cd ($rs->all) {
55 my $data->{year} = $cd->year;
56 for my $tr ($cd->tracks->all) {
57 push @{$data->{tracks}}, { $tr->get_columns };
59 push @cds_and_tracks, $data;
62 my $pref_rs = $rs->search ({}, { columns => ['year'], prefetch => 'tracks' });
64 my @pref_cds_and_tracks;
65 for my $cd ($pref_rs->all) {
66 my $data = { $cd->get_columns };
67 for my $tr ($cd->tracks->all) {
68 push @{$data->{tracks}}, { $tr->get_columns };
70 push @pref_cds_and_tracks, $data;
74 \@pref_cds_and_tracks,
76 'Correct collapsing on non-unique primary object'
80 [ $pref_rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all ],
82 'Correct HRI collapsing on non-unique primary object'
85 }, 'weird collapse lives');
89 # test implicit prefetch as well
91 my $rs = $schema->resultset('CD')->search(
92 { title => 'Generic Manufactured Singles' },
95 select => [qw/ me.title artist.name / ],
100 is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
101 isa_ok ($cd->artist, 'DBICTest::Artist');
102 is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
104 }, 'implicit keyless prefetch works');