[Catalyst] Overriding $c->model / selecting different databases

Bill Moseley moseley at hank.org
Fri Apr 29 21:05:34 GMT 2011

I'm not sure if this is more of a DBIC question or a Catalyst question.

I have two Catalyst::Model::DBIC::Schema model classes called
"App::Model::Foo" and "App::Model::Bar" configured to use two different
schema classes.  The databases are not exactly the same but both DBIC schema
classes expose the same methods needed for some actions.  That is, a
Foo::User and Bar::User object look similar and both have all the methods
that an action might end up calling on a user object.  But, the actual
database schema might different between the two -- different table name or
different set of columns.

The existing controllers use $c->model( 'Foo::User' ), for example.  So,
what I need is a way to switch models based on some value in the request
(e.g. inspecting the Host header) to $c->model( 'Bar::User' ).

I could use this rather crude approach:

around model =3D> sub {
    my $model =3D shift;
    my $self =3D shift;
    my $type =3D shift;

    $type =3D~ /^Foo/Bar/ if $self->request->header( 'Host' ) =3D~

    return $self->$model( $type, @_ );

Is there a better approach for selecting different models?  I suppose
another way would be to have App::Model::Foo select the schema class to


-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20110429/6b1c3=

More information about the Catalyst mailing list