Re: How to use a model with a different table name

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to use a model with a different table name

Jonathan Rockway
* On Mon, Jun 23 2008, Tobias Kremer wrote:

> Quoting Emmanuel Quevillon <[hidden email]>:
>> I have a module DB::GenericTable.pm which reflects a
>> 'generic' table schema into my database associated to a
>> table name in the DB.
>> I'd like to kow how can I access another table (different
>> name) but with the same schema (same column name, same
>> datatype) from my perl module? Someting like
>
> I'd say by making a base table class and subclassing from it:
>
> package DB::GenericTable;
> use strict;
> use warnings;
> use base 'DBIx::Class';
> __PACKAGE__->load_components("Core");
> __PACKAGE__->table("genericname");
> __PACKAGE__->add_columns(...);
> 1;
>
> package DB::DerivedTable;
> use strict;
> use warnings;
> use base 'DB::GenericTable';
> __PACKAGE__->table("othertable");
> 1;

No, that won't work.  Why would __PACKAGE__ in DB::GenericTable have
anything to do with DB::DerivedTable?

Try this instead:

  package Superclass;
  use base 'DBIx::Class';

  sub setup_table {
     my ($class, @args) = @_;
     $class->load_components('Core');
     ...
  }

  package Subclass;
  use base 'Superclass';
 
  __PACKAGE__->setup_table( ... );

Note that you don't need to use inheritance for this either, you can
have a helper module make the tables for you.  See DBICx::MapMaker for
example.

Regards,
Jonathan Rockway

--
print just => another => perl => hacker => if $,=$"

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Loading...