NAME

CGI::Application::Plugin::DBIC::Schema - Easy DBIx::Class access from CGI::Application, inspired by CGI::Application::Plugin::DBH.

SYNOPSIS

use CGI::Application::Plugin::DBIC::Schema (qw/dbic_config schema resultset rs/);

sub cgiapp_init{

        my $c = shift;

        my $dsn = $c->param('dsn');
        my $dbpw = $c->param('dbpw');
        my $dbuser = $c->param('dbuser');

        # Provide a default config.

        $c->dbic_config({schema=>"My::DB",                  # DBIC class
                         connect_info=>[$dsn,$dbuser,$dbpw] # use same args as DBI connect
                        });


}


sub setup {

        my $c = shift;

        $c->start_mode('runmode1');
        $c->run_modes([qw/runmode1 runmode2/]);
}

sub runmode1 {

        my $c = shift;

        my $id = $c->param('id);
        $c->resultset("My::DB::DemoTable")->find($id);
        
        # do something with the object ...

        return "found it.";
}

sub runmode2 {

        my $c = shift;
        
        $c->schema()->resultset("My::DB::DemoTable")
            ->create({name=>"John Doe", address=>"Any Street"});

        return "created it";
}

DESCRIPTION

CGI::Application::Plugin::DBIC::Schema adds easy access to a DBIx::Class::Schema to your Titanium or CGI::Application modules. Lazy loading is used to prevent a database connection from being made if the schema method is not called during the request. In other words, the database connection is not created until it is actually needed.

DBIx::Class has lots of dependencies, and therefore a certain length of compile time, but it works fine in a CGI environment for low volume sites. If you expect a high volume of traffic, think about FastCGI or other alternatives.

METHODS

schema($name?)

This method will return the default DBIx::Class::Schema instance if no name is provided. Provide a schema name to retrieve an alternate schema. The schema instance is created on the first call to this method, and any subsequent calls will return the same instance.

my $schema = $c->schema();                 # gets default (unnamed) schema

# Or ...

my $schem = $c->schema('my_schema_name');  # gets one of named schemas

dbic_config($name?, \%connect_info)

Used to provide your DBIx::Class::Schema class name, an optional config name, and DBI connection parameters. For \%config_info supply the same parameter list that you would for DBI::connect. You may also supply DBIx::Class specifig attributes. For that see DBIx::Class::Storage::DBI for details.

The recommended place to call dbic_config is in the cgiapp_init stage of CGI::Application. If this method is called after the dbic() method has already been accessed, then it will die with an error message.

        # Setup  default schema config
        
	$c->dbic_config({schema=>"My::DB",                  # DBIC class
			 connect_info=>[$dsn,$dbuser,$dbpw] # use same args as DBI connect
			});

	# Or, provide additional configs by name.

	$c->dbic_config("another_config",
			{schema=>"My::Other::DB",
			 connect_info=>[$dsn,$dbuser,$dbpw]
			});

resultset($config_name?,$resultset_classname)

An alias to $c->schema(...)->resultset(...).

This method provides DBIx::Class::Resultset access.

# Use the default dbic schema via 'resultset'. 

$c->resultset("DBICT::Result::Test")->find($id);


# Or use a named config to access resultset via an alternative schema.

$c->resultset('another_config', "DBICT::Result::Test")->find($id);

# Or use alias short form, 'rs' with default config

$c->rs("DBICT::Result::Test")->find($id);

# Or use alias short form with alternate config/schema

$c->rs('yet_another_schema', "DBICT::Result::Test")->find($id);

rs

An alias to resultset

SEE ALSO

DBIx::Class, Titanium, CGI::Application,CGI::Application::Plugin::DBH

AUTHOR

Gordon Van Amburg <gordon@minipeg.net>

LICENSE

Copyright (C) 2009 Gordon Van Amburg <gordon@minipeg.net>

This library is free software. You can modify and or distribute it under the same terms as Perl itself.