<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-GB link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='color:black'>Dear All,<o:p></o:p></span></p><p class=MsoNormal><span style='color:black'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='color:black'>I&#8217;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&#8217;t have the smarts to understand the documentation, could someone please explain some things to me?<o:p></o:p></span></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'>Essentially I reckon I want to change the __PACKAGE__-&gt;config in my model on a per transaction basis.&nbsp; My current model looks like this<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>package EasyAC::Model::DB;<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>use strict;<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>use base 'Catalyst::Model::DBIC::Schema';<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>__PACKAGE__-&gt;config(<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp; schema_class =&gt; 'EasyAC::Schema',<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp; <o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;connect_info =&gt; {<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dsn =&gt; 'dbi:mysql:easyac01',<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user =&gt; 'me',<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; password =&gt; 'mypass',<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AutoCommit =&gt; q{1},<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp; }<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>);<o:p></o:p></span></p><p class=MsoNormal><span style='color:black'>The ACCEPT_CONTEXT documentation suggests:<o:p></o:p></span></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-size:10.5pt'>Add a field to </span><span style='font-size:10.0pt;font-family:"Courier New"'>$c</span><span style='font-size:10.5pt'>, like </span><span style='font-size:10.0pt;font-family:"Courier New"'>my_model_instance</span><span style='font-size:10.5pt'>. Then write your </span><span style='font-size:10.0pt;font-family:"Courier New"'>ACCEPT_CONTEXT</span><span style='font-size:10.5pt'> method to look like this:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>sub</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>ACCEPT_CONTEXT { </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;my</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>( $self, $c</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>) = @_; </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:9.5pt'>&nbsp;<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;if</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>( my</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>$per_request</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>= $c-&gt;my_model_instance ) { </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;return</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>$per_request; </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;} else</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>{ </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;my</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>$new_instance</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>= bless</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>{ %$self, c</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>=&gt; $c</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>}, ref($self); </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;Scalar::Util::weaken($new_instance-&gt;{c}); # or we have a circular reference </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;$c-&gt;my_model_instance( $new_instance</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>); </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;&nbsp;&nbsp;return</span><span style='font-size:9.5pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>$new_instance; </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>&nbsp;&nbsp;} </span><span style='font-size:9.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='color:black'>Question 1: how do I add a field like my_model_instance and from which component do I add it?&nbsp; I did try obvious things like $c-&gt;my_model_instance = {}.&nbsp; </span><span style='font-size:10.5pt'><o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.5pt'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='color:black'>Question 2:&nbsp; How do I replace the static connect_info with the per request info (I have dsn, user and password in the stash).&nbsp; I&#8217;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?<o:p></o:p></span></p><p class=MsoNormal><span style='color:black'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='color:black'>I&#8217;ve also looked at the documentation for <a href="https://metacpan.org/module/Catalyst::Component::InstancePerContext"><span style='color:black;text-decoration:none'>Catalyst::Component::InstancePerContext</span></a>.&nbsp; Which I cannot understand either:<o:p></o:p></span></p><p class=MsoNormal><span style='color:black'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='color:black'>Question 3:&nbsp; 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-&gt;new(%args).&nbsp; Does this then replace the connect_info in MyApp::Model::DB?<o:p></o:p></span></p><p class=MsoNormal><span style='color:black'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='color:black'>Question 4:&nbsp; 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?<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>Regards,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>Scott<o:p></o:p></span></p></div></body></html>