[Dbix-class] has_many relationships
Aran Deltac
aran at arandeltac.com
Mon May 1 21:43:51 CEST 2006
On 5/1/06, Dan Horne <dan.horne at redbone.co.nz> wrote:
> Hi all
>
> I'm don't quite understand how to do joins in a "has many" context, and why
> it works differently from a "belongs_to" equivalent. Take, for example the
> music schema in Example.pod
> (http://search.cpan.org/~elliott/DBIx-Class-0.06002/lib/DBIx/Class/Manual/Ex
> ample.pod). The following query works (where CDs have many Tracks):
>
> 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";
> }
>
> This all works, but if I try to add the track title to the query:
>
> print $cd->tracks->title . "\n\n";
$cd->tracks() returns a resultset. If you want to get at the title of
the tracks do something like this:
my $tracks_rs = $cd->tracks();
while (my $track = $tracks_rs->next()) {
print( $track->title() . "\n" );
}
> I get:
>
> Can't locate object method "title" via package "DBIx::Class::ResultSet"
>
> How do I also get the track title from the resultset?
A resultset by definition is a set, not a single record. So,
retrieving a single field on a resultset is not a logical approach.
Aran
> Dan
>
>
> _______________________________________________
> 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/
>
More information about the Dbix-class
mailing list