[Catalyst] How to use dbh in the model

Richard Jones ra.jones at dpw.clara.co.uk
Tue Jan 29 11:29:42 GMT 2008


I'm playing with a hand-crafted model using Catalyst::Model::DBI and 
DBIx::Simple. In one of the methods I create a $dbi object as 
DBIx::Simple->new($self->dbh), and then stuff $dbi into the 
$self->context->stash for use elsewhere in the model methods, and all is 
well.

But if that particular method isn't called by a Controller, then the 
DBIx::Simple object isn't created and subsequent methods fail. I thought 
I ought to be able to do something like this at the top instead:

package MyApp::Model::DBI;

__PACKAGE__->config(
   dbi => DBIx::Simple->new(__PACKAGE__->dbh),
);

but obviously not:
Can't use string ("MyApp::Model::DBI") as a HASH ref while "strict refs" 
in use at /usr/local/share/perl/5.8.7/Class/Accessor/Fast.pm line 38.

MyApp::Model::DBI connect_info is automatically loaded from the global 
config file. I need to get the models dbh into DBIx::Simple->new(). What 
am I doing wrong?
-- 
Richard Jones



More information about the Catalyst mailing list