The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


Catalyst::Model::DBI - DBI Model Class


  # use the helper to create a model for example
  perl script/ model MyModel DBI dsn username password

  # lib/MyApp/Model/
  package MyApp::Model::DBI;

  use base 'Catalyst::Model::DBI';

    dsn           => 'DBI:Pg:dbname=mydb;host=localhost',
    username      => 'pgsql',
    password      => '',
    options       => { AutoCommit => 1 },
    loglevel      => 1


  # or load settings from a config file via Config::General for example
  # in your myapp.conf you could have

  name MyApp

    dsn "DBI:Pg:dbname=mydb;host=localhost"
    username pgsql
    password ""
      AutoCommit 1
    loglevel 1

  # note that config settings always override Model settings

  # do something with $dbh inside a controller ...
  my $dbh = $c->model('MyModel')->dbh;

  # do something with $dbh inside a model ...
  my $dbh = $self->dbh;

  #do something with DBIx::Connector connection inside a controller ...
  my $connection = $c->model('MyModel')->connection;

  #do something with DBIx::Connector connection inside a model ...
  my $connection = $self->connection;


This is the DBI model class. It has been rewritten to use DBIx::Connector since it's internal code that deals with connection maintenance has already been ported into there. You now have two options for doing custom models with Catalyst. Either by using this model and any related modules as needed or by having your custom model decoupled from Catalyst and glued on using Catalyst::Model::Adaptor

Some general rules are as follows. If you do not wish to use DBIx::Connector directly or DBI and setup connections in your custom models or have glue models, then use this model. If you however need models that can be re-used outside of your application or simply wish to maintain connection code yourself outside of the Catalyst, then use Catalyst::Model::Adaptor which allows you to glue outside models into your Catalyst app.



Initializes DBI connection


Returns the current DBIx::Connector connection handle.


Returns the current database handle.


Connects to the database and returns the handle.


Catalyst, DBI, Catalyst::Model::Proxy, Catalyst::Model::DBI::SQL::Library


Alex Pavlovic,


Copyright (c) 2005 - 2012 the Catalyst::Model::DBI "AUTHOR" as listed above.


This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.