[Catalyst] Creating a thin Model

Jamie Neil jamie at versado.net
Tue May 22 12:56:56 GMT 2007


Matt S Trout wrote:
> If you get stuck, could you start a fresh thread, please? I think this one
> has officially got confused now :)

Ok. Just for the record though, this seems to be working fine so far:


package MySite::Model::Widget;

use strict;
use warnings;
use base qw/Catalyst::Model/;
use MySite::Widget;

__PACKAGE__->config(
     connect_info => [
         'dbi:Pg:mysite', 'username',
         'password', { AutoCommit => 1 },
     ],
);

sub new {
     my ( $class, $c, $args ) = @_;
     return MySite::Widget->new(
         Catalyst::Utils::merge_hashes( $args, $class->config ) );
}

1;


package MySite::Widget;

use warnings;
use strict;

use base qw/Class::Accessor/;
use MySite::Schema::DB;
use Carp;

__PACKAGE__->mk_accessors(qw/schema/);

sub new {
     my $self = shift->next::method(@_);

     croak "->connect_info must be defined"
       unless $self->{connect_info};

     # Clone and connect to schema
     $self->{'schema'} =
       MySite::Schema::DB->connect( @{ $self->{connect_info} } );

     return $self;
}

sub find {
     my ( $self, $id ) = @_;
     return $self->{'schema'}->resultset('Widgets')->find($id);
}
...


package MySite::Controller::Widget;
...

# Get widget
my $widget = $c->model('Widget')->find($id);
$c->stash->{'widget'} = $widget;
...

-- 
Jamie Neil | <jamie at versado.net> | 0870 7777 454
Versado I.T. Services Ltd. | http://versado.net/ | 0845 450 1254



More information about the Catalyst mailing list