[Catalyst] How to use CDBI model outside Catalyst

Bill Moseley moseley at hank.org
Tue Dec 6 15:00:36 CET 2005

Here's what I do.

I have a normal CDBI parent class, but it also includes this:

    package DB;
    use strict;
    use warnings;
    use base 'Class::DBI::Sweet';

    sub new {
        my $class = shift;
        my %params = ref $_[0] ? %{$_[0]} : @_;

        $params{dsn} ||= 'dbi:Pg:dbname=test';

        $params{options} ||= {};

        $params{options}{AutoCommit} = 1;

        my @connection = @params{ qw/ dsn user password options /};

        __PACKAGE__->connection( @connection );

        return bless \%params, $class;

    [... common model methods ...]

Then in Catalyst I have this:

    package App::M::CDBI;
    use strict;
    use DB;
    DB->new( App->config->{database} );

Which also works in other programs.

By the way, in my parent class above I have a list of all my cdbi
classes and use UNIVERSAL::require to load them, so loading the "DB"
example above loads all the individual classes.  I suspect there's
other other ways to do that.

Bill Moseley
moseley at hank.org

More information about the Catalyst mailing list