logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Catalyst::Model::DBI - DBI Model Class

Author

Description

       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.

License

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

perl v5.34.0                                       2022-06-09                          Catalyst::Model::DBI(3pm)

Methods

       new Initializes DBI connection

       $self->connection
           Returns the current DBIx::Connector connection handle.

       $self->dbh
           Returns the current database handle.

       $self->connect
           Connects to the database and returns the handle.

Name

       Catalyst::Model::DBI - DBI Model Class

See Also

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

Synopsis

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

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

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

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

         1;

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

         name MyApp

         <Model::MyModel>
           dsn "DBI:Pg:dbname=mydb;host=localhost"
           username pgsql
           password ""
           <options>
             AutoCommit 1
           </options>
           loglevel 1
         </Model>

         # 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;

See Also