[Dbix-class] Problems getting prefetch to work..

Tim Watson tiwatson at gmail.com
Sat Jun 24 19:37:21 CEST 2006


I think I was going about this a little reverse...

This works...

    my $rs = $dbh->resultset('User')->search({ 'sessionid' => '123456'
} ,{ prefetch => 'user_session' });

But I am unable to figure out how to access anything else in that
session row... since I am returned a user object by $rs->next;

I would like to be able to get a session obect and the user object
from that one line..

-timw


On 6/24/06, Alex Beamish <talexb at gmail.com> wrote:
> On 6/24/06, Tim Watson <tiwatson at gmail.com> wrote:
> >
> > I am probably missing something simple here, but I can't see it.
> >
> > I am having trouble getting any prefetching to work, in my own code or
> > in the example code linked here:
> >
> >
> >
> http://search.cpan.org/~jrobinson/DBIx-Class-0.06003/lib/DBIx/Class/Manual/Example.pod
> >
> > With the example, only changing the DB type to be mysql and my
> > login...   I get an sql error, the JOIN section is not working
> > properly....
> >
> > 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 = ? )
> >
> > In my own code... equally as simple..
> >
> > -----------
> > package Db::Schema::User;
> >
> > use strict;
> >
> > use base qw/DBIx::Class/;
> >
> > __PACKAGE__->load_components(qw/PK::Auto Core/); # for example
> > __PACKAGE__->table('user');
> > __PACKAGE__->add_columns(qw/ username password /);
> > __PACKAGE__->set_primary_key('username');
> > __PACKAGE__->has_many(user_session => 'Db::Schema::Session', 'username');
> >
> > ----------------
> >
> > package Db::Schema::Session;
> >
> > use strict;
> >
> > use base qw/DBIx::Class/;
> >
> > __PACKAGE__->load_components(qw/PK::Auto Core/); # for example
> > __PACKAGE__->table('session');
> > __PACKAGE__->add_columns(qw/sessionid username /);
> > __PACKAGE__->set_primary_key('sessionid');
> > __PACKAGE__->belongs_to(username => 'Db::Schema::User');
> >
> >
> > -----------------
> >
> >
> > my $rs = $dbh->resultset('Session')->search({ 'session.sessionid' =>
> > '123456' } ,{ prefetch => 'username' });
> > my $session = $rs->next;
> > print "s " . $session->sessionid . "\n";
> >
> > (prefetching on anything but username gives me a relationship error)
>
>
> Did you try prefetching on user_session?
>
> I've been having problems with this part of the package as well, and I'm
> still working on how to improve this part of the documentation.
>
> In my mind, the naming goes from Database Tables -> Relationships (has_many,
> and so forth) -> ResultSet calls. In the POD, the database table called 'cd'
> has a relationship called 'tracks' that refers to the 'track' table, for
> example, which explains why the resultset call uses 'tracks' and not
> 'track'.
>
> Now, that's not carved in stone -- I still need to make a few more visits
> through the code using the debugger, to get it all figured out, but that's
> what I've found out so far, and I'm eager to share more knowledge.
>
> Good luck.
>
> --------
> >
> > SELECT me.sessionid, me.username, username.username, username.password
> > FROM session me  JOIN user username ON ( username.username =
> > me.username ) WHERE ( session.sessionid = ? )'
> >
> > I get the same JOIN errors...   Any idea what I am doing wrong ??
> >
> > --
> > Tim Watson
> >
> > _______________________________________________
> > 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/
> >
>
>
>
> --
> Alex Beamish
> Toronto, Ontario
>
>



More information about the Dbix-class mailing list