[Dbix-class] how to select from two tables ?

Will Hawes wdhawes at gmail.com
Sat Sep 15 16:55:26 GMT 2007


On 15/09/2007, Jason Kohles <email at jasonkohles.com> wrote:
>
> On Sep 15, 2007, at 10:21 AM, Silent wrote:
>
> > Hi~
> >     I am new to dbix-class also SQL, in my test script I want to
> > select from two tables:
> >
> > sqlite> .schema
> > CREATE TABLE author (
> > uid integer primary key,
> > uname text not null
> > );
> > CREATE TABLE news (
> > newsid integer primary key,
> > uid integer not null references author(uid),
> > title text not null,
> > body text not null
> > );
> >
> > if ($dump) {
> >     my @news =3D $schema->resultset('News')->all(
> >         {
> >             join =3D> [qw / author /],
> >         }
> >       );
> >     for my $row (@news) {
> >         print $row->title,"\n", $row->uid, "\n", $row->body,"\n";
> > # how can I get the author name here ?
> >     }
> >
>
> $row->uid->uname, although you probably want to rename the
> relationship so it makes more sense, by including
>
> My::Schema::News->belongs_to( 'author', 'My::Schema::Author', 'uid' );
>
> Then you can get it as;
>
> $row->author->uname;


You might also want to add a "prefetch" attribute to your search to cut down
on the number of SQL statements issued as you loop through the results. See
http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/ResultSet.pm#prefetch=
and
http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod#U=
sing_joins_and_prefetchfor
details.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20070915/00c=
8f530/attachment.htm


More information about the DBIx-Class mailing list