[Dbix-class] Example confusion -- my own

Alex Beamish talexb at gmail.com
Thu Jun 22 18:34:22 CEST 2006


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.

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?

Feedback greatly appreciated. Thanks!

-- 
Alex Beamish (talexb)
Toronto, Ontario
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/dbix-class/attachments/20060622/45b1a983/attachment.htm 


More information about the Dbix-class mailing list