phildeman has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Trouble with Join/Prefetch in DBIx::Class
by duff (Parson) on Jan 09, 2018 at 23:57 UTC | |
Re: Trouble with Join/Prefetch in DBIx::Class
by Mr. Muskrat (Canon) on Jan 10, 2018 at 16:56 UTC | |
Re: Trouble with Join/Prefetch in DBIx::Class
by poj (Abbot) on Jan 10, 2018 at 20:12 UTC |