[Dbix-class] One class, multiple tables: create on the fly

Oleg Kostyuk cub.uanic at gmail.com
Tue Oct 27 17:39:46 GMT 2009


Hello all,

I have a lot of similar tables, and need to access them via
DBIx::Class. I don't want create separate module for each table,
because there is lot of tables, and also this will not be DRY. Also, I
can't give DBIx::Class create classes for me on startup, because some
new tables can be created by other parts of system on the fly, and I
need to have possibility to query these new tables too. I searched
thru list for possible solution, and found similar (but not exactly
same) question:
http://www.mail-archive.com/dbix-class@lists.scsys.co.uk/msg03062.html
. At the end, mst@ says
(http://www.mail-archive.com/dbix-class@lists.scsys.co.uk/msg03068.html):

<cite>
You don't need a modification. Just create a result source object per
table on the fly and register them as using the same class.
</cite>

What is correct way to do this?

Currently, I already have schema model and class for base table (with
all needed columns and relations defined), in Model::DB and
Model::DB::BaseTable, respectively. I want to have some
Model::TableFactory, which will create new result sources, exactly
same as Model::DB::BaseTable, except they should use different tables.
What is correct way to create result sources on the fly and write
Model::TableFactory?

Finally, I want to use TableFactory like this (in Catalyst application):

my $tbl1 = $c->req->params->{table};
my $rs1  = $c->model('TableFactory')->table($tbl1)->search(...)->all;
my $tbl2 = $c->req->params->{table2};
my $rs2  = $c->model('TableFactory')->table($tbl2)->search(...)->count;

Of course, before use $tbk1 and $tbk2, I need to be sure that they
don't have illegal characters, represent existing tables etc etc -
this is skipped for simplicity here.

May be, you can propose different/better solution?
Any help welcome.
Thanks in advance.

--
Sincerely yours,
Oleg Kostyuk (CUB-UANIC)



More information about the DBIx-Class mailing list