[Catalyst] Accessing DB from external model

Hernan Lopes hernanlopes at gmail.com
Fri Nov 5 11:43:27 GMT 2010


Mike,

also, if you havent already, take a look on Catalyst::Tutorial , in my
opinion its by far the best documentation to begin with catalyst.

http://search.cpan.org/~zarquon/Catalyst-Manual-5.8005/lib/Catalyst/Manual/Tutorial/01_Intro.pod



On 11/5/10, Mike Raynham <catalyst at mikeraynham.co.uk> wrote:
> On 05/11/10 10:10, Octavian Rasnita wrote:
>> From: "Mike Raynham" <catalyst at mikeraynham.co.uk>
>>> Hi,
>>>
>>> I am fairly new to all things Perl and Catalyst, and would like to
>>> know if what I am attempting to do is correct.
>>>
>>> I've recently finished reading 'The Definitive Guide to Catalyst',
>>> which has been very helpful, but I am still a little confused about
>>> separating my business logic from my Catalyst application, and
>>> accessing the database connection from my non-Catalyst application.
>>>
>>> At the moment, I have my external class in the lib/MyApp directory. It
>>> uses the connection info found in MyApp::Model::DB, like so:
>>>
>>> ---
>>>
>>> # lib/MyApp/MyExternalClass.pm
>>> MyApp::MyExternalClass
>>>
>>> use MyApp::Model::DB;
>>> use MyApp::Schema;
>>>
>>> my $connect_info = MyApp::Model::DB->config->{connect_info};
>>> my $schema = MyApp::Schema->connect( $connect_info );
>>>
>>> # Do stuff with $schema...
>>>
>>> ---
>>>
>>> That works, and I can connect to the database. However, I have
>>> searched this mailing list, and found this:
>>>
>>> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/msg00817.html
>>>
>>> Here, the connection information is moved from MyApp::Model::DB to
>>> MyApp::DB, and then Catalyst::Model::Adaptor is used to glue
>>> MyApp::Model::DB to MyApp::DB. Is it a good idea? MyApp::Model::DB
>>> appears to be part of the Catalyst application, so if I want to access
>>> the database from an external model, it makes sense to me to move the
>>> connection code outside of the Catalyst application.
>>>
>>>
>>> Regards,
>>>
>>> Mike
>>
>>
>> The best idea would be to put the connection information in the
>> application's config file like in the example below. (This example uses
>> a Perl data structure, but you can use any configuration type accepted
>> by Config::Any).
>>
>> 'Model::DB' => {
>> schema_class => 'MyApp::Schema',
>> connect_info => {
>> dsn => 'dbi:Oracle:host=10.10.10.10;port=1521;sid=ora8',
>> user => 'user',
>> password => "password",
>> #name_sep => '.',
>> LongReadLen => 100*1024*1024,
>> LongTruncOk => 1,
>> on_connect_call => 'datetime_setup',
>> on_connect_do => [
>> "alter session set NLS_COMP='LINGUISTIC'",
>> "alter session set NLS_SORT='BINARY_AI'",
>> ],
>> },
>> },
>>
>> The model will access the connection information directly if it is
>> defined in the config file, and you can use the data from this config
>> file in any other external program.
>> You can use the module Config::JFDI for using the Catalyst config file
>> easier.
>>
>> Octavian
>
> Hi,
>
> Thanks for the speedy response.  When you put it like like, it all seems
> so simple and obvious :-)
>
> Config::JFDI looks handy - I'll take a look at that.
>
>
> Regards,
>
> Mike
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>



More information about the Catalyst mailing list