[Catalyst] Access to DBIx schema from outside Catalyst

Dermot paikkos at googlemail.com
Tue Dec 8 08:33:55 GMT 2009


2009/12/8 J. Shirley <jshirley at gmail.com>:
> On Mon, Dec 7, 2009 at 4:21 PM, Greg Matheson <drbean at freeshell.org> wrote:

>> > > 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?
>>

> That would only work if he is using Catalyst, and a Catalyst component.
> This is somewhat the other direction.
>
> He wants to access the DBIx::Class schema outside of Catalyst and simply
> have access to ->log and ->debug methods.

Indeed. I wasn't really intending to use Moose but it's hard to avoid
it these days so a big thanx for the code example and link. I think
that will be more appropriate than use lib.
Dp.



More information about the Catalyst mailing list