[Catalyst] Access to DBIx schema from outside Catalyst

Greg Matheson drbean at freeshell.org
Tue Dec 8 00:21:54 GMT 2009


On Mon, 07 Dec 2009, J. Shirley wrote:

> On Mon, Dec 7, 2009 at 8:50 AM, Eden Cardim <edencardim at gmail.com> wrote:

> > >>>>> "Dermot" == Dermot  <paikkos at googlemail.com> writes:

> >    Dermot> If I have a Class outside my Cat App, I can have access to
> >    Dermot> that via C::Model::Adapator. Is there a method for giving
> >    Dermot> that external Class access to Catalyst features,
> >    Dermot> particularly the DBix schema and perhaps the debug and log
> >    Dermot> methods?

> > Both DBIx::Class::Schema and Catalyst::Log are regular perl classes
> > and you can use them anywhere in a perl program just like you'd use any
> > other class.



> Or, more verbosely:

> package MyApp::External::Class;

> use Moose;

> has 'schema' =>  ( is => 'ro', isa => 'DBIx::Class::Schema', handles => {
> 'resultset' => 'resultset' } );
> has 'log'         => ( is => 'ro', isa => 'Catalyst::Log', default => sub {
> Catalyst::Log->new } );
> has 'debug'    => ( is => 'ro', isa => 'Bool' );
> 1;

> I do things like this (though not with Catalyst::Log) quite frequently, and
> just create a custom type for the schema[1], add a coercion and can pass in
> a array ref to call ->connect:

> my $schema = MyApp::External::Class->new( schema => [ $dsn, $user, $pass ],
> debug => 1 );
> $schema->resultset('Foo"); # handles does this.

I've seen Catalyst::Component::InstancePerContext recommended on 
IRC to get the $c context object, but this is more than needed?





More information about the Catalyst mailing list