[Catalyst-dev] Bug in Catalyst::Model::DBI .15
Evan Carroll
lists at evancarroll.com
Mon May 7 08:34:49 GMT 2007
On 5/7/07, Alex Pavlovic <alex at taskforce-1.com> wrote:
> Well, one way of getting to the $dbh before was through the ACCEPT_CONTEXT
> because if you had single Root DBI model, you could just do:
>
> my $dbh = $c->model('Root')->dbh;
This is essentially the way I'm doing it.
> from a model that did not inherit from that root model. The other alternative
> was to use C::M::Proxy instead. This way each model could get access to
> shared $dbh. There is no relation with the $c->user.
> Each time you inherit from a single root model, a database connection has to
> be created for that specific child model. This is not a big overhead though,
> because it's done only once per model, unless the request is in a different
> process or thread ( which is a different story ).
Why is that? The non-Root model's don't inherit from M::DBI at all..
They inherit from M::DBI::Root which uses a single instance of M::DBI
> So to cut the story short, unless you re-implement similiar code used by the
> Proxy in your Root model, then you will still be opening connection initially
> each time you inherit, i.e. there will be separate connection channel for
> each model.
I still don't understand what exactly M::Proxy does, let me show my code.
packagepackage Dealermade::M::DBI;
use base qw/Catalyst::Model::DBI/;
use strict;
use warnings;
use Data::Dumper;
use Carp;
__PACKAGE__->config(
dsn => 'dbi:Pg:dbname=dealermade;host=127.0.0.1',
user => 'ecarroll',
password => 'barfoo',
options => { AutoCommit => 1 },
);
sub ACCEPT_CONTEXT {
my( $self, $c ) = @_;
bless { %$self, c => $c}, ref ( $self );
}
1;
Dealermade::M::DBI::Root;
use base 'Catalyst::Component';
sub ACCEPT_CONTEXT {
my( $self, $c ) = @_;
bless { %$self, c => $c}, ref ( $self );
}
sub dbh {
my $self = shift;
$self->{c}->model('DBI')->dbh;
}
package Dealermade::M::DBI::Contact::Pots;
use base 'Dealermade::M::DBI::Root';
sub create {
my ( $self, $args ) = @_;
my $dbh = $self->dbh;
unless ( $self->{_is_valid} ) {
Carp::croak 'Failed validation'
unless $self->validate($args)
;
}
my $sth = $dbh->prepare('
INSERT INTO contact.pots ( number, fkey_user )
VALUES ( ?, ? )
');
$sth->execute(
$args->{number}
, $self->user_id
);
Carp::croak $sth->errstr if $sth->err;
Carp::croak 'No modifications' unless $sth->rows;
}
(Has been cut down to show only what is needed)
--
Evan Carroll
System Lord of the Internets
me at evancarroll.com
832-445-8877
More information about the Catalyst-dev
mailing list