FAQ
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.

Search Discussions

  • Hans Staugaard at Jun 10, 2011 at 6:15 am
    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@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/
  • John Karr at Jun 11, 2011 at 10:46 pm
    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@tweakker.com]
    Sent: Friday, June 10, 2011 2:15 AM
    To: catalyst@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@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@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/
  • John Karr at Jun 11, 2011 at 11:54 pm
    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
    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 [h.staugaard@tweakker.com]
    Sent: Friday, June 10, 2011 2:15 AM
    To: catalyst@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@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@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@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/
  • Andrew Rodland at Jun 12, 2011 at 5:04 am

    On Saturday, June 11, 2011 07:54:46 PM John Karr wrote:
    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.
    Do absolutely nothing. It already works that way. That's what Catalyst::Model
    (really Catalyst::Component) provides.
  • Eden Cardim at Jun 12, 2011 at 2:52 pm
    "John" == John Karr writes:
    John> The mistake in my code brings up the other question (which applies to ORM
    John> models as well): How to get the model to take its parameters from
    John> <appname>.conf instead of having to input them directly in the model.

    It already works that way, try:

    <Model::RSVP>
    dsn 'DBI:Pg:dbname=rsvp;host=joomla.brain.buz',
    user joomla
    password 007drupal
    <Model>

    --
    Eden Cardim Need help with your Catalyst or DBIx::Class project?
    Code Monkey http://www.shadowcat.co.uk/catalyst/
    Shadowcat Systems Ltd. Want a managed development or deployment platform?
    http://blog.edencardim.com/ http://www.shadowcat.co.uk/servers/
    http://twitter.com/#!/edenc

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedJun 10, '11 at 12:39a
activeJun 12, '11 at 2:52p
posts6
users4
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2021 Grokbase