[Dbix-class] Prefetching resulset subqueries

thilo.fester@googlemail.com thilo.fester at gmail.com
Sat Feb 14 20:23:00 GMT 2015


2015-02-14 12:57 GMT+01:00 Peter Rabbitson <rabbit+dbic at rabbit.us>:

>
> Note that the above usage is unsupported (specifying a from structure by
> hand may or may not work this way). [...] this *will* stop working in the
> distant future.
>

Normally I don't use it. I just did to find a solution for this special
case and because ... ^_^ ... the documentation (still) says sub queries are
supported with the from keyword ->
https://metacpan.org/pod/DBIx::Class::Manual::Cookbook#Support

But thanks for that information, I will avoid it. Is it appropriate to
contribute a fix for the Cookbook's POD or should I consult somebody before?



> In essence your best bet is to define a real relationship to "tracklist"
> with the correct subquery and everything, like in this example:
> https://github.com/dbsrgits/dbix-class/blob/current/for_
> cpan_index/t/lib/DBICTest/Schema/CD.pm#L120-L154
>
> If you are interested in the inner logic that collapse => 1 triggers, it
> can be found in this "double-recursion" function:
> https://github.com/dbsrgits/dbix-class/blob/current/for_
> cpan_index/lib/DBIx/Class/ResultSource/RowParser.pm#L149-L398
>

Thanks for pointing out those lines. I wrote a relationship very similar to
"last_track" in the CD test case and it does the job:

    __PACKAGE__->has_many(
        'tracklist',
        'MyApp::Schema::Result::Track',
        sub {
            my $args = shift;

            return (
                {
                    "$args->{foreign_alias}.trackid" => { '-in' =>

$args->{self_resultsource}->schema->resultset('Track')->search(
                            { 'cd.artist' => { -ident =>
"$args->{self_alias}.artistid" } },
                            {
                                join => 'cd',
                                alias => 'correlated_tracks',
                                columns => ['trackid']
                            },
                        )->as_query
                    }
                }
            );
        },
    );

Now I can use the "prefetch" keyword in the search call.



> If this reply isn't sufficient to answer your question - please ask
> further.

Your answer is very helpful and constructive! Thank you very much : )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20150214/f1003486/attachment.htm>


More information about the DBIx-Class mailing list