Eric Wright wrote:
I have what I hope is not a dumb question. I'm trying to follow best
practices here and am writing a piece of business logic that I feel
should be Catalyst agnostic and therefore am creating a custom model.
(However, at the moment Catalyst is the primary consumer of this lib.)
I'd like to use my DBIC::Schema goodness for DB access and happily
have abstracted that via Catalyst::Model::DBIC::Schema. The thing is I
realized that if I connect to this schema outside of the scope of
Catalyst via DBIx::Class::Schema->connect I have to pass in the
connection info separately which really bugs me because I've already
defined it via a configuration file for the Catalyst app (using
Config::General). I suppose I could just pass that info into the model
but that seems messy and redundant to me. If I subclass
Catalyst::Model well then I'm tightly coupled with Catalyst again. I'd
like to just be able to glue the model to Catalyst and do a:
$c->model('MyCustomModel')->foo(...)
and it just work.
It seems to me there should be a simple way for this schema to know
its connection info based on context and I figured there's like a
slick Catalyst ninja kind of method for doing this that my brain is
just not grasping at the moment. How do you do this sort of thing? Is
the magic in the Catalyst glue?
Thanks in advance for any advice/thoughts/comments...
-Eric
I use an external yaml file to hold connection information. In that way
I can easily have different
configurations for different environments, dev, staging, live.
# production database access
DBIxProd:
dsn: 'DBI:mysql:host=localhost;database=myapp_prod'
username: fred
password: secret
...
In my Catalyst model I do something like.
package myapp::Model::DBIxProd;
use strict;
use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->config(
schema_class => 'myapp::Storage',
connect_info => [
myapp->config->{DBIxProd}{dsn},
myapp->config->{DBIxProd}{username},
myapp->config->{DBIxProd}{password},
{ 'mysql_enable_utf8' => 1 },
{ on_connect_do =>[ 'set names utf8' ] },
]);
1;
And in my external app (usually in a 'tools' subdirectory) I do
something like.
use FindBin;
use lib "$FindBin::Bin/../lib";
use myapp::Storage;
use YAML;
my $config = YAML::LoadFile("$FindBin::Bin/../myapp.yml");
my $schema = myapp::Storage->connect(
$config->{DBIxProd}{dsn},
$config->{DBIxProd}{username},
$config->{DBIxProd}{password},
{ mysql_enable_utf8 => 1 },
{ on_connect_do => [ 'set names utf8' ] },
);
Regards
Ian
_______________________________________________
List: Catalyst@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/