<div>I'm not sure if this is more of a DBIC question or a Catalyst question.</div><div><br></div>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.<div>
<br></div><div>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' ).</div>
<div><br></div><div><br></div><div>I could use this rather crude approach:</div><div><br></div><div>around model => sub {<br> my $model = shift;<br> my $self = shift;<br> my $type = shift;<br><br> $type =~ /^Foo/Bar/ if $self->request->header( 'Host' ) =~ /$bar_host_re/;<br>
<br> return $self->$model( $type, @_ );<br>};</div><div><br></div><div><br></div><div>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 return.</div>
<div><br></div><div>Thanks,<br><div><br></div><div><br></div><div>-- <br>Bill Moseley<br><a href="mailto:moseley@hank.org" target="_blank">moseley@hank.org</a><br>
</div></div>