[Catalyst] Access to DBIx schema from outside Catalyst

J. Shirley jshirley at gmail.com
Mon Dec 7 17:13:12 GMT 2009


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

> >>>>> "Dermot" =3D=3D Dermot  <paikkos at googlemail.com> writes:
>
>    Dermot> Hi, I haven't finished reading the book yet so I hope this
>    Dermot> question won't be answered there.
>
>    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' =3D>  ( is =3D> 'ro', isa =3D> 'DBIx::Class::Schema', handles =
=3D> {
'resultset' =3D> 'resultset' } );
has 'log'         =3D> ( is =3D> 'ro', isa =3D> 'Catalyst::Log', default =
=3D> sub {
Catalyst::Log->new } );
has 'debug'    =3D> ( is =3D> 'ro', isa =3D> '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 =3D MyApp::External::Class->new( schema =3D> [ $dsn, $user, $pas=
s ],
debug =3D> 1 );
$schema->resultset('Foo"); # handles does this.

Ok, so this is more Moose-pimping than anything, but this recipe works well
for me.

[1] Read about MooseX::Types to create a custom type with a coercion at
http://search.cpan.org/~rkitover/MooseX-Types-0.20/lib/MooseX/Types.pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20091207/6d6f2=
929/attachment.htm


More information about the Catalyst mailing list