Search code examples
perldbix-class

DBIX Class- same result class for multiple tables


I have many tables in my database which have the same structure. I want to create a common result class (package DBIx::Class) for that set of tables and use the same class for all by somehow changing the table name on the fly.

How can I do this?

EDIT Encouraged by the answer from @abraxxa I am extending the same question. In my database I have a set of tables for many customers which have common structure. After creating a common set of result classes I want to use the same for all customers in following fashion.

customer 1

tableA_1 tableB_1 tableC_1

customer N

tableA_N tableB_N tableC_N

while working on customer N, I want to manage tableA, tableB and tableC such that their relationships are also maintained. Like, if I access tableB from within tableA, then for customer 1 it should access tableB_1 and for customer N tableB_N should be accessed.

Can you please outline a base class for tableA and then one the subclass for the same.


Solution

  • Create a Result base class and subclass it for each existing table only calling

    __PACKAGE__->table('tablename');
    

    in each subclass. DBIx::Class still needs to know about all existing tables because of their relationships.