I am having difficulty trying to retrieve rows of data when using prefetch or join.

PERSON TABLE PACKAGE CONTAINS:

__PACKAGE__->has_many( "tbl_requests", "DB::MyDBS::Result::TblRequests", { "foreign.PID" => "self.PID" }, { cascade_copy => 0, cascade_delete => 0 }, );

When making the query from the app:

my @requests = $schema->resultset( 'TblPerson' )->search({ LastName => 'Smith' +, FirstName => 'John' + }, { prefetch => 'tbl_requests' } ); foreach my $request (@requests) { print "Name: " . $request->LastName . ", " .$request->FirstName . +"\n"; print "SternID: " . $request->username . "\n"; print "PrefName: " . $request->PrefName . "\n"; print "Status: " . $request->tbl_requests->status . " . "\n"; }

This produces the following error:

Name: Smith, John
Username: js12345
Can't locate object method "status" via package "DBIx::Class::ResultSet" at test_requests.pl line 18

Line 18 in the test script is: print "Status: " . $request->tbl_requests->status . " . "\n";

Now this works fine when I do the reverse:

__PACKAGE__->belongs_to( "p", "DB::MyDBS::Result::TblPerson", { PID => "PID" }, { is_deferrable => 1, on_delete => "NO ACTION", on_update => "NO ACT +ION" }, );

When making the query from the app:

my @requests = $schema->resultset( 'TblRequests' )->search({ 'p.LastName' => 'Smith', 'p.FirstName' => 'John' }, { prefetch => 'p' } ); foreach my $request (@requests) { print "Name: " . $request->p->LastName . ", ". $request->p->FirstN +ame . "\n"; print "SternID: " . $request->p->username . "\n"; print "Status: " . $request->status . " . "\n"; }

Any thoughts why the first scenario gives the error and the second does not?


In reply to Trouble with Join/Prefetch in DBIx::Class by phildeman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.