1 package Catalyst::Model::DBIC::Schema;
4 use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
6 use UNIVERSAL::require;
11 __PACKAGE__->mk_classaccessor('composed_schema');
12 __PACKAGE__->mk_accessors('schema');
16 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
20 package MyApp::Model::Foo;
22 use base 'Catalyst::Model::DBIC::Schema';
25 schema_class => 'Foo::SchemaClass',
26 connect_info => [ 'dbi:Pg:dbname=foodb',
37 # ->schema To access schema methods:
38 $c->model('Foo')->schema->source(...);
40 # certain ->schema methods (source, resultset, class) have shortcuts
41 $c->model('Foo')->source(...);
42 $c->model('Foo')->resultset(...);
43 $c->model('Foo')->class(...);
45 # For resultsets, there's an even quicker shortcut:
47 # is the same as $c->model('Foo')->resultset('Bar')
49 # To get the composed schema for making new connections:
50 my $newconn = $c->model('Foo')->composed_schema->connect(...);
52 # Or the same thing via a convenience shortcut:
53 my $newconn = $c->model('Foo')->connect(...);
55 # or, if your schema works on different storage drivers:
56 my $newconn = $c->model('Foo')->composed_schema->clone();
57 $newconn->storage_type('::LDAP');
58 $newconn->connection(...);
60 # and again, a convenience shortcut
61 my $newconn = $c->model('Foo')->clone();
62 $newconn->storage_type('::LDAP');
63 $newconn->connection(...);
67 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
68 the documentation for L<Catalyst::Helper::Model::DBIC::Schema> and
69 L<Catalyst::Helper::Model::DBIC::SchemaLoader> for information
70 on generating these Models via Helper scripts. The latter of the two
71 will also generated a L<DBIx::Class::Schema::Loader>-based Schema class
74 =head1 CONFIG PARAMETERS
80 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
81 to be findable in C<@INC>, but it does not need to be underneath
82 C<Catalyst::Model::>. This parameter is required.
86 This is an arrayref of connection parameters, which are specific to your
87 C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
88 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
89 dsn, username, password, and connect options hashref.
91 This is not required if C<schema_class> already has connection information
92 defined in itself (which would be the case for a Schema defined by
93 L<DBIx::Class::Schema::Loader>, for instance).
97 Allows the use of a different C<storage_type> than what is set in your
98 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
99 L<DBIx::Class>). Completely optional, and probably unnecessary for most
100 people until other storage backends become available for L<DBIx::Class>.
110 Instantiates the Model based on the above-documented ->config parameters.
111 The only required parameter is C<schema_class>. C<connect_info> is
112 required in the case that C<schema_class> does not already have connection
113 information defined for it.
117 Accessor which returns the connected schema being used by the this model.
118 There are already direct shortcuts on the model class itself for
119 schema->resultset, schema->source, and schema->class.
121 =item composed_schema
123 Accessor which returns the composed schema, which has no connection info,
124 which was used in constructing the C<schema> above. Useful for creating
125 new connections based on the same schema/model. There are direct shortcuts
126 from the model object for composed_schema->clone and composed_schema->connect
130 Shortcut for ->composed_schema->clone
134 Shortcut for ->composed_schema->connect
138 Shortcut for ->schema->source
142 Shortcut for ->schema->class
146 Shortcut for ->schema->resultset
150 Provides an accessor for the connected schema's storage object.
151 Used often for debugging and controlling transactions.
158 my $self = shift->NEXT::new(@_);
160 my $class = ref($self);
161 my $model_name = $class;
162 $model_name =~ s/^[\w:]+::(?:Model|M):://;
164 croak "->config->{schema_class} must be defined for this model"
165 unless $self->{schema_class};
167 my $schema_class = $self->{schema_class};
169 $schema_class->require
170 or croak "Cannot load schema class '$schema_class': $@";
172 if( !$self->{connect_info} ) {
173 if($schema_class->storage && $schema_class->storage->connect_info) {
174 $self->{connect_info} = $schema_class->storage->connect_info;
177 croak "Either ->config->{connect_info} must be defined for $class"
178 . " or $schema_class must have connect info defined on it";
182 $self->composed_schema($schema_class->compose_namespace($class));
183 $self->schema($self->composed_schema->clone);
184 $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
185 $self->schema->connection(@{$self->{connect_info}});
188 foreach my $moniker ($self->schema->sources) {
189 my $classname = "${class}::$moniker";
190 *{"${classname}::ACCEPT_CONTEXT"} = sub {
192 shift->model($model_name)->resultset($moniker);
199 sub clone { shift->composed_schema->clone(@_); }
201 sub connect { shift->composed_schema->connect(@_); }
203 sub storage { shift->schema->storage(@_); }
207 General Catalyst Stuff:
209 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
210 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
212 Stuff related to DBIC and this Model style:
214 L<DBIx::Class>, L<DBIx::Class::Schema>,
215 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
216 L<Catalyst::Helper::Model::DBIC::SchemaLoader>
220 Brandon L Black, C<blblack@gmail.com>
224 This program is free software, you can redistribute it and/or modify it
225 under the same terms as Perl itself.