[Catalyst] DBI Models Question.
John Karr
brainbuz at brainbuz.org
Sat Jun 11 22:46:27 GMT 2011
Thanks Hans, that answered the question!
Here is some proof of concept code:
=== Model ====
package DBIS::Model::RSVP;
use strict; use warnings;
use DBIx::Simple;
use parent 'Catalyst::Model::DBI';
__PACKAGE__->config(
dsn => 'DBI:Pg:dbname=rsvp;host=joomla.brain.buz',
user => 'joomla',
password => '007drupal',
on_connect_do => q{PRAGMA foreign_keys = ON},
);
use Moose ; #use Moose immediately before calling on Moose to extend the object.
has db=>(
is =>'ro',
isa=>'DBIx::Simple',
lazy_build=> 1,
# If we don't want to handle all dbis methods, specify those that we want.
# handles=> [qw/query flat /],
);
sub _build_db {
my $self = shift ;
return DBIx::Simple->connect($self->dbh);
}
sub Count {
my $self = shift ;
my $q = 'SELECT COUNT (*) FROM guest' ;
( my $count ) = $self->db->query( $q )->flat ;
return $count ;
}
1;
=== Controller ===
package DBIS::Controller::Root;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller' }
__PACKAGE__->config(namespace => '');
sub index :Path :Args(0) {
my ( $self, $c ) = @_;
my $msg = "<pre>Demonstration of a DBIx::Simple based Model!\n" ;
# Use the count method for maximum re-usability.
my $guests = $c->model('RSVP')->Count() ;
$msg .= "We have $guests guests!\n" ;
# Directly access the DBIx::Simple object in your controller for trivial queries.
my $q = 'SELECT COUNT (*) FROM guest' ;
( my $guests2 ) = $c->model('RSVP')->db->query( $q )->flat ;
$msg .= "We still have $guests2 guests!\n" ;
$c->response->body( "$msg</pre>" );
}
1;
________________________________________
From: Hans Staugaard [h.staugaard at tweakker.com]
Sent: Friday, June 10, 2011 2:15 AM
To: catalyst at lists.scsys.co.uk
Subject: Re: [Catalyst] DBI Models Question.
Hi John
I think I would just add something like this to the model:
has db => (
is =>'ro',
isa => 'DBIx::Simple',
lazy_build=> 1,
handles => [qw/query select insert delete/], # Etc.
);
sub _build_db {
my $self = shift;
return DBIx::Simple->connect($self->dbh);
}
then you should be able to use $self->db, $self->query etc in your subs.
Regards, Hans
On 2011-06-10 02:39, John Karr wrote:
>
> I've been dabbling with catalyst for a while and working with
> Model::DBI. There are a few issues that I haven't figured out to my
> satisfaction.
>
> loading the dbi connection strings from myapp.conf
>
> extending the dbi model with DBIx::Simple. My current method is to
> create a new dbixsimple object from $self->dbh in every subroutine, I
> would like to get to a point where this is set up in setup of each dbi
> model and accessible in a manner like either: my $db = $self->db or
> $self->do_some_dbixsimple_method( @arguments ).
>
> I'd also be happy to hear some views on:
> pointers on good coding for dbi models -- the balance between
> reusability and not ending up with a method in your model for every
> possible query.
>
> =====
>
> The discussion I am attempting to start is about DBI models and not
> the merits of ORM vs DBI, but about doing DBI models well.
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
_______________________________________________
List: Catalyst at lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/
More information about the Catalyst
mailing list