The mistake in my code brings up the other question (which applies to ORM models as well): How to get the model to take its parameters from <appname>.conf instead of having to input them directly in the model.
Fortunately that was just a test server and no big deal to change its passwords, but nearly every time I copy a catalyst application from one server to another server I need to edit the model. And the problem is obvious (and as shown by my post easy to mess up) if you are sharing some model code via irc or a mailing list. -----Original Message----- From: John Karr [mailto:[email protected]] Sent: Saturday, June 11, 2011 6:46 PM To: The elegant MVC web framework Subject: RE: [Catalyst] DBI Models Question. 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 [[email protected]] Sent: Friday, June 10, 2011 2:15 AM To: [email protected] 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: [email protected] > Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst > Searchable archive: > http://www.mail-archive.com/[email protected]/ > Dev site: http://dev.catalyst.perl.org/ > _______________________________________________ List: [email protected] Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/[email protected]/ Dev site: http://dev.catalyst.perl.org/ _______________________________________________ List: [email protected] Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/[email protected]/ Dev site: http://dev.catalyst.perl.org/ _______________________________________________ List: [email protected] Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/[email protected]/ Dev site: http://dev.catalyst.perl.org/
