Changes
lib/Catalyst/Helper/Model/DBIC/Schema.pm
lib/Catalyst/Model/DBIC/Schema.pm
+Makefile.PL
MANIFEST This list of files
+META.yml
+README
t/01use.t
t/02pod.t
t/03podcoverage.t
-META.yml
-Makefile.PL
-README
# ->schema To access schema methods:
$c->model('Foo')->schema->source(...);
- # Shortcut to the schema resultset monikers for ->search et al:
- $c->model('Foo::Bar')->search(...);
- # is the same as $c->model('Foo')->schema->resultset('Bar')->search(...);
+ # certain ->schema methods (source, resultset, class) have shortcuts
+ $c->model('Foo')->source(...);
+ $c->model('Foo')->resultset(...);
+ $c->model('Foo')->class(...);
+
+ # For resultsets, there's an even quicker shortcut:
+ $c->model('Foo::Bar')
+ # is the same as $c->model('Foo')->resultset('Bar')
# To get the composed schema for making new connections:
my $newconn = $c->model('Foo')->composed_schema->connect(...);
schema_class
This is the classname of your DBIx::Class::Schema Schema. It needs
to be findable in @INC, but it does not need to be underneath
- "Catalyst::Model::".
+ "Catalyst::Model::". This parameter is required.
connect_info
This is an arrayref of connection parameters, which are specific to
parameters are your dsn, username, password, and connect options
hashref.
+ This is not required if "schema_class" already has connection
+ information defined in itself (which would be the case for a Schema
+ defined by DBIx::Class::Schema::Loader, for instance).
+
storage_type
Allows the use of a different "storage_type" than what is set in
your "schema_class" (which in turn defaults to "::DBI" if not set in
METHODS
new Instantiates the Model based on the above-documented ->config
- parameters.
+ parameters. The only required parameter is "schema_class".
+ "connect_info" is required in the case that "schema_class" does not
+ already have connection information defined for it.
schema
Accessor which returns the connected schema being used by the this
- model.
+ model. There are already direct shortcuts on the model class itself
+ for schema->resultset, schema->source, and schema->class.
composed_schema
Accessor which returns the composed schema, which has no connection
info, which was used in constructing the "schema" above. Useful for
- creating new connections based on the same schema/model.
+ creating new connections based on the same schema/model. There are
+ direct shortcuts from the model object for composed_schema->clone
+ and composed_schema->connect
clone
Shortcut for ->composed_schema->clone
connect
Shortcut for ->composed_schema->connect
+ source
+ Shortcut for ->schema->source
+
+ class
+ Shortcut for ->schema->class
+
+ resultset
+ Shortcut for ->schema->resultset
+
SEE ALSO
Catalyst, DBIx::Class, DBIx::Class::Schema, DBIx::Class::Schema::Loader
package Catalyst::Helper::Model::DBIC::Schema;
use strict;
+use warnings;
+use Carp;
=head1 NAME
=head1 SYNOPSIS
- script/create.pl model Foo DBIC::Schema Foo::SchemaClass dsn user password
+ script/create.pl model Foo DBIC::Schema Foo::SchemaClass [ dsn user password ]
+
+ Where:
+ Foo is the short name for the Model class being generated
+ Foo::SchemaClass is the fully qualified classname of your Schema,
+ which isa DBIx::Class::Schema defined elsewhere.
+ dsn, user, and password are optional if connection info is already
+ defined in your Schema class (as it would be in the case of
+ DBIx::Class::Schema::Loader).
=head1 DESCRIPTION
=cut
sub mk_compclass {
- my ( $self, $helper, $schemaclass, $dsn, $user, $pass ) = @_;
- $helper->{schemaclass} = $schemaclass || '';
- $helper->{dsn} = $dsn || '';
- $helper->{user} = $user || '';
- $helper->{pass} = $pass || '';
+ my ( $self, $helper, $schema_class, $dsn, $user, $pass ) = @_;
+
+ $helper->{schema_class} = $schema_class || '';
+
+ if(defined($dsn)) {
+ $helper->{setup_connect_info} = 1;
+ $helper->{dsn} = $dsn || '';
+ $helper->{user} = $user || '';
+ $helper->{pass} = $pass || '';
+ }
+
my $file = $helper->{file};
$helper->render_file( 'compclass', $file );
}
use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->config(
- schema_class => '[% schemaclass %]',
+ schema_class => '[% schema_class %]',
+ [% IF setup_connect_info %]
connect_info => [ '[% dsn %]',
'[% user %]',
'[% pass %]',
AutoCommit => 1,
}
],
+ [% END %]
);
=head1 NAME
This is the classname of your L<DBIx::Class::Schema> Schema. It needs
to be findable in C<@INC>, but it does not need to be underneath
-C<Catalyst::Model::>.
+C<Catalyst::Model::>. This parameter is required.
=item connect_info
in L<DBIx::Class> at the time of this writing, the 4 parameters are your
dsn, username, password, and connect options hashref.
+This is not required if C<schema_class> already has connection information
+defined in itself (which would be the case for a Schema defined by
+L<DBIx::Class::Schema::Loader>, for instance).
+
=item storage_type
Allows the use of a different C<storage_type> than what is set in your
=item new
Instantiates the Model based on the above-documented ->config parameters.
+The only required parameter is C<schema_class>. C<connect_info> is
+required in the case that C<schema_class> does not already have connection
+information defined for it.
=item schema
my $model_name = $class;
$model_name =~ s/^[\w:]+::(?:Model|M):://;
- foreach (qw/ connect_info schema_class /) {
- croak "->config->{$_} must be defined for this model"
- unless $self->{$_};
- }
+ croak "->config->{schema_class} must be defined for this model"
+ unless $self->{schema_class};
my $schema_class = $self->{schema_class};
$schema_class->require
or croak "Cannot load schema class '$schema_class': $@";
+ if( !$self->{connect_info} ) {
+ if($schema_class->storage && $schema_class->storage->connect_info) {
+ $self->{connect_info} = $schema_class->storage->connect_info;
+ }
+ else {
+ croak "Either ->config->{connect_info} must be defined for $class"
+ . " or $schema_class must have connection defined on it";
+ }
+ }
+
$self->composed_schema($schema_class->compose_namespace($class));
$self->schema($self->composed_schema->clone);
$self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
return $self;
}
-# convenience method
sub clone { shift->composed_schema->clone(@_); }
-# convenience method
sub connect { shift->composed_schema->connect(@_); }
=head1 SEE ALSO