[Dbix-class] Example confusion -- my own

Justin Guenther jguenther at gmail.com
Thu Jun 22 22:57:38 CEST 2006


On 6/22/06, Alex Beamish <talexb at gmail.com> wrote:
> Hi,
>
> I'm just getting up to speed on DBIx::Class as a precursor to doing Great
> Things (I hope) with Catalyst. When I do that kind of thing, I like to do
> all of the examples so I can really understand them. It all makes sense with
> the exception of the join in the result set call.
>
> First, the example that makes sense:
>
>   #  SELECT me.trackid, me.cd, me.title, cd.cdid, cd.artist, cd.title
>   #    FROM track me
>   #    JOIN cd cd ON ( cd.cdid = me.cd )
>   #   WHERE ( cd.title = ? )
>   #  (`Bad')
>
>   sub get_tracks_by_cd {
>     my $cdtitle = shift;
>     print "get_tracks_by_cd($cdtitle):\n";
>     my $rs = $schema->resultset('Track')->search(
>       {
>         'cd.title' => $cdtitle
>       },
>       {
>         join     => [qw/ cd /],
>         # prefetch => [qw/ cd /]    # Not needed?
>       }
>     );
>     while (my $track = $rs->next) {
>       print $track->title . "\n";
>     }
>     print "\n";
>   }
>
> No problem -- you're searching for tracks, with a join on the cd table. I've
> commented out the prefetch on cd, since the example just prints out the
> track title. I've added the SQL that gets generated in comments. Note that
> the alias is the same as the real table name. Cool.

Note that it's only the same as the real table name because the
relationship `cd' is the same as its table name.

> Here's the first one that has me stumped ..
>
>   #     SELECT me.cdid, me.artist, me.title
>   #       FROM cd me
>   #  LEFT JOIN track tracks ON ( tracks.cd = me.cdid )
>    #      WHERE ( tracks.title = ? )
>   #  (`Stan')
>
>   sub get_cd_by_track {
>     my $tracktitle = shift;
>     print "get_cd_by_track($tracktitle):\n";
>     my $rs = $schema->resultset('Cd')->search(
>       {
>         'tracks.title' => $tracktitle
>       },
>       {
>         join     => [qw/ tracks /],
>       }
>     );
>     my $cd = $rs->first;
>     print $cd->title . "\n\n";
>    }
>
> .. now we're joining on tracks instead of track, and I don't understand why.
> I tried to understand this on IRC and I guess either didn't make myself
> understood, or I'm thicker than two bricks. Note that the alias makes this
> work .. tracks is an alias for track. When I tried changing tracks back to
> track in the two places they appear, I got an error. Why does tracks work
> and track does not?

This is because the alias names in DBIx::Class are either (1) `me',
for the current table, or (2) the _relationship_ name (or if more than
1 occurrence of the same rel occurs in the query, the following ones
are named $rel_2 etc.)

So join => 'tracks' doesn't mean join to the `tracks' table, it means
join to the `tracks' relationship, which happens to exist on the
`track' table. Just like in the first example you gave `cd' didn't
refer to a table, it referred to a relationship that just so happened
to be named the same as the table.

> Feedback greatly appreciated. Thanks!
>
> --
> Alex Beamish (talexb)
> Toronto, Ontario
>
>
> _______________________________________________
> List:
> http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN:
> http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> Searchable Archive:
> http://www.mail-archive.com/dbix-class@lists.rawmode.org/
>
>



More information about the Dbix-class mailing list