I am trying to use a common base class for multiple result classes in DBIX::Class. Reason is - I have few tables with same structure but different names.
Here is my base class
use utf8;
package myapp::Schema::tablebase;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table("unknown");
__PACKAGE__->add_columns(
"id",
{ data_type => "smallint", is_nullable => 0 }
#, ... and lot more
);
Here is actual result class
package myapp::Schema::Result::ActualTable;
use base 'myapp::Schema::tablebase';
# Correct table name
__PACKAGE__->table('patient2');
1;
I am getting compilation error for this effort. Please help me on this.
Update:
The error I am getting is -
DBIx::Class::Schema::catch {...} (): Attempt to load_namespaces() class myapp::Schema::Result::ActualTable failed - are you sure this is a real Result Class?: Can't locate object method "result_source_instance" via package "myapp::Schema::Result::ActualTable" at C:/Strawberry/perl/site/lib/DBIx/Class/Schema.pm line 195. at C:/Strawberry/perl/site/lib/myapp/Schema.pm
Here are the changes I made to methods "subclass" and "generate_relationships", to preserve various relationships among global and client specific tables. Also I had to drop reverse relationships form global to many client specific tables.
For each subclass I am passing client_id as argument, which is blank for global tables.
That should work, maybe because your base class is missing the true return value (1;) at the end?
You can also use DBIx::Class::Helper::Row::SubClass if you prefer a neater solution that also fixes relationships that your base class might have defined.