[Catalyst] Models Inheriting from Catalyst::Base

Brandon Black blblack at gmail.com
Thu Nov 17 18:29:08 CET 2005


On 11/17/05, Nigel Metheringham
<nigel.metheringham at dev.intechnology.co.uk> wrote:
> I see in the Catalyst::Manual::Cookbook page
>   http://search.cpan.org/~mramberg/Catalyst-5.56/lib/Catalyst/Manual/Cookbook.pod#Using_existing_CDBI_(etc.)_classes_with_Catalyst
> (the Using existing CDBI (etc.) classes with Catalyst section), that its
> suggested that Catalyst::Base be added to the base class of a Model.
>
> However I used the recipe in
>   http://dev.catalyst.perl.org/wiki/SolvedIssues#si.85
> (Getting the most from DBIx::Class::Loader) as a basis for my Model
> classes (I actually specify the DBIC classes manually as not all are on
> standard tables).  This does not inherit from Catalyst::Base
>
> Are there good reasons why I should add this additional inheritance (at
> the potential cost of making some of the other scripts pull in chunks of
> Catalyst when I don't need it)?  What terrible things will happen if I
> don't fix this?

That example came from me, and I don't really know what I'm doing, so
don't take it too seriously :)

AFAIK, the advantage of using Catalyst base classes in your model is
that your ::Loader configuration can come from Catalyst-derived
->config() directives, which in turn means you can set your database
dsn, password, etc from your application YAML config, instead of
hardcoding it in the Model file.

Here's a copy of what mine looks like at the moment, which is using a
Catalyst base class for no good reason at all at the moment (but I'll
soon have a good reason, once I eliminate the use of Singleton which
you should ignore).  The explicit ->config() literals here could
instead be in my main application YAML:

package MyWeb::M::MyDB;

use strict;

use base 'Catalyst::Model::DBIC';
use NEXT;
use DateTime;

__PACKAGE__->config(
    dsn           => 'dbi:Pg:dbname=vdmc',
    user          => 'postgres',
    password      => '',
    options       => { AutoCommit => 1 },
    exclude       => '_[0-9]+$',
    relationships => 1,
    namespace     => __PACKAGE__,
);

sub new {
    my ( $self, $c ) = @_;
    $self = $self->NEXT::new($c);

    my $loader = $self->loader;
    foreach my $class ($loader->classes) {
        # Make sure the class loads correctly
        eval " use $class; ";
        if($@) { if($@ !~ /^Can't locate /) { die $@ } }

        # Add automatic conversion to/from perl DateTime
        # objects for all _stamp columns in the database,
        # which are unix epoch timestamp integers.
        # Uses Catalyst::Singleton for session dttz access
        $class->inflate_column(
            $_, {
                inflate => sub {
                    my $c = MyWeb->context;
                    my $tz = $c ? $c->req->{dttz} : undef ;
                    DateTime->from_epoch(
                        epoch => $_[0],
                        time_zone => $tz || 'UTC',
                    )
                },
                deflate => sub { $_[0]->epoch },
            }
        ) for grep { /_stamp$/ } $class->columns;
    }

    ($loader->classes)[0]->storage->dbh->disconnect;

    return $self;
}

1;



More information about the Catalyst mailing list