
scott at simpzoid
May 15, 2012, 12:11 PM
Post #1 of 2
(121 views)
Permalink
|
|
Authentication in a Many Database Scenario (again)
|
|
Dear All, I'd like to offer users their own database (so that their own data is separate from other users) but use the same MVC code for all the user databases. Following some helpful advice from this forum I have found the documentation for ACCEPT_CONTEXT but unfortunately I don't have the smarts to understand the documentation, could someone please explain some things to me? Essentially I reckon I want to change the __PACKAGE__->config in my model on a per transaction basis. My current model looks like this package EasyAC::Model::DB; use strict; use base 'Catalyst::Model::DBIC::Schema'; __PACKAGE__->config( schema_class => 'EasyAC::Schema', connect_info => { dsn => 'dbi:mysql:easyac01', user => 'me', password => 'mypass', AutoCommit => q{1}, } ); The ACCEPT_CONTEXT documentation suggests: Add a field to $c, like my_model_instance. Then write your ACCEPT_CONTEXT method to look like this: sub ACCEPT_CONTEXT { my ( $self, $c ) = @_; if ( my $per_request = $c->my_model_instance ) { return $per_request; } else { my $new_instance = bless { %$self, c => $c }, ref($self); Scalar::Util::weaken($new_instance->{c}); # or we have a circular reference $c->my_model_instance( $new_instance ); return $new_instance; } } Question 1: how do I add a field like my_model_instance and from which component do I add it? I did try obvious things like $c->my_model_instance = {}. Question 2: How do I replace the static connect_info with the per request info (I have dsn, user and password in the stash). I'm guessing that I put the connect_info into the hashref which I pass to ACCEPT_CONTEXT (i.e. the hashref which becomes $self) and that Catalyst just magically resolves all my worries? I've also looked at the documentation for <https://metacpan.org/module/Catalyst::Component::InstancePerContext> Catalyst::Component::InstancePerContext. Which I cannot understand either: Question 3: when the InstancePerContext documentation says # ... do your thing here , I take it this means that I create a hash with my connect info and return MyApp::Model::DB->new(%args). Does this then replace the connect_info in MyApp::Model::DB? Question 4: Should I give up as none of the above are remotely correct and I obviously lack the intelligence to use a sophisticated tool like Catalyst? Regards, Scott
|