sgundry has asked for the wisdom of the Perl Monks concerning the following question:
This search works. Yes, I can probably achieve this without the custom result source thingo but that is beside the point (for now). But it returns the expected rows when I calluse base 'DBIx::Class'; __PACKAGE__->load_components("Core"); __PACKAGE__->table("Locations"); __PACKAGE__->add_columns( "locationid", { data_type => "integer", is_nullable => 0, size => undef }, "name", { data_type => "string", is_nullable => 0, size => undef }, ); __PACKAGE__->set_primary_key("locationid"); __PACKAGE__->has_many( "npcs", "fhl::Schema::Npcs", { "foreign.locationid" => "self.locationid" }, ); __PACKAGE__->has_many( "incidents", "fhl::Schema::Incidents", { "foreign.locationid" => "self.locationid" }, ); ... my $source = __PACKAGE__->result_source_instance(); my $new_source = $source->new($source); $new_source->source_name( 'DB::Locations::NumberActiveIncidents' ); $new_source->name( \<<SQL ); ( SELECT l.*, count(i.incidentid) as NumIncidents FROM Incidents i, Locations l WHERE i.locationid = l.locationid AND i.enddatetime > ? GROUP BY l.locationid ORDER BY NumIncidents DESC ) SQL fhl::Schema->register_source( 'DB::Locations::NumberActiveIncidents' = +> $new_source );
An 'Incidents' row contains, among others, a 'LocationID' which is a row from the 'Locations' table above. Before adding the above code I could do, say,my $active = [ $schema->resultset('Locations::NumberActiveIncidents')- +>search( {}, { bind => [ $now ] } )];
That is, I could access the locations row via the incidents locationid value. However, now when I try the above I get an error:my $active = [$schema->resultset('Incidents::Active')->search({}, { bi +nd => [ $now ] } )]; foreach my $incident (@$active) { print $incident->description, " ", $incident->locationid->name, "\n"; }
The culprit line is:Use of uninitialized value in subroutine entry at c:/Perl/site/lib/DBI +x/Class/Storage/DBI.pm line 1003. DBIx::Class::InflateColumn::get_inflated_column(): DBI Exception: DBD: +:SQLite::db prepare_cached failed: unrecognized token: "0x2270f9c"(1) + at dbdimp.c line 271 [for Statement "INSERT INTO SCALAR(0x2270f9c) ( +locationid) VALUES (?)"] at (eval 970) line 6
If I leave him out obviously the search does not work but the incident.locationid.name works. Please help. Any assistance suggestions would be great. Thanks, Samfhl::Schema->register_source( 'DB::Locations::NumberActiveIncidents' = +> $new_source );
|
|---|