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',
31 on_connect_do => [ 'sql statement 1', 'sql statement 2' ],
38 # ->schema To access schema methods:
39 $c->model('Foo')->schema->source(...);
41 # certain ->schema methods (source, resultset, class) have shortcuts
42 $c->model('Foo')->source(...);
43 $c->model('Foo')->resultset(...);
44 $c->model('Foo')->class(...);
46 # For resultsets, there's an even quicker shortcut:
48 # is the same as $c->model('Foo')->resultset('Bar')
50 # To get the composed schema for making new connections:
51 my $newconn = $c->model('Foo')->composed_schema->connect(...);
53 # Or the same thing via a convenience shortcut:
54 my $newconn = $c->model('Foo')->connect(...);
56 # or, if your schema works on different storage drivers:
57 my $newconn = $c->model('Foo')->composed_schema->clone();
58 $newconn->storage_type('::LDAP');
59 $newconn->connection(...);
61 # and again, a convenience shortcut
62 my $newconn = $c->model('Foo')->clone();
63 $newconn->storage_type('::LDAP');
64 $newconn->connection(...);
68 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
69 the documentation for L<Catalyst::Helper::Model::DBIC::Schema> and
70 L<Catalyst::Helper::Model::DBIC::SchemaLoader> for information
71 on generating these Models via Helper scripts. The latter of the two
72 will also generated a L<DBIx::Class::Schema::Loader>-based Schema class
75 =head1 CONFIG PARAMETERS
81 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
82 to be findable in C<@INC>, but it does not need to be underneath
83 C<Catalyst::Model::>. This parameter is required.
87 This is an arrayref of connection parameters, which are specific to your
88 C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
89 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
90 dsn, username, password, and connect options hashref.
92 This is not required if C<schema_class> already has connection information
93 defined in itself (which would be the case for a Schema defined by
94 L<DBIx::Class::Schema::Loader>, for instance).
98 This is an arrayref of sql statements, which are executed on every connect.
99 May not be a valid/useful argument with non-DBI-based Storages.
103 Allows the use of a different C<storage_type> than what is set in your
104 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
105 L<DBIx::Class>). Completely optional, and probably unnecessary for most
106 people until other storage backends become available for L<DBIx::Class>.
116 Instantiates the Model based on the above-documented ->config parameters.
117 The only required parameter is C<schema_class>. C<connect_info> is
118 required in the case that C<schema_class> does not already have connection
119 information defined for it.
123 Accessor which returns the connected schema being used by the this model.
124 There are already direct shortcuts on the model class itself for
125 schema->resultset, schema->source, and schema->class.
127 =item composed_schema
129 Accessor which returns the composed schema, which has no connection info,
130 which was used in constructing the C<schema> above. Useful for creating
131 new connections based on the same schema/model. There are direct shortcuts
132 from the model object for composed_schema->clone and composed_schema->connect
136 Shortcut for ->composed_schema->clone
140 Shortcut for ->composed_schema->connect
144 Shortcut for ->schema->source
148 Shortcut for ->schema->class
152 Shortcut for ->schema->resultset
156 Provides an accessor for the connected schema's storage object.
157 Used often for debugging and controlling transactions.
164 my $self = shift->NEXT::new(@_);
166 my $class = ref($self);
167 my $model_name = $class;
168 $model_name =~ s/^[\w:]+::(?:Model|M):://;
170 croak "->config->{schema_class} must be defined for this model"
171 unless $self->{schema_class};
173 my $schema_class = $self->{schema_class};
175 $schema_class->require
176 or croak "Cannot load schema class '$schema_class': $@";
178 if( !$self->{connect_info} ) {
179 if($schema_class->storage && $schema_class->storage->connect_info) {
180 $self->{connect_info} = $schema_class->storage->connect_info;
183 croak "Either ->config->{connect_info} must be defined for $class"
184 . " or $schema_class must have connect info defined on it";
188 $self->composed_schema($schema_class->compose_namespace($class));
189 $self->schema($self->composed_schema->clone);
191 $self->schema->storage_type($self->{storage_type})
192 if $self->{storage_type};
193 $self->schema->connection(@{$self->{connect_info}});
194 $self->schema->storage->on_connect_do($self->{on_connect_do})
195 if $self->{on_connect_do};
198 foreach my $moniker ($self->schema->sources) {
199 my $classname = "${class}::$moniker";
200 *{"${classname}::ACCEPT_CONTEXT"} = sub {
202 shift->model($model_name)->resultset($moniker);
209 sub clone { shift->composed_schema->clone(@_); }
211 sub connect { shift->composed_schema->connect(@_); }
213 sub storage { shift->schema->storage(@_); }
217 General Catalyst Stuff:
219 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
220 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
222 Stuff related to DBIC and this Model style:
224 L<DBIx::Class>, L<DBIx::Class::Schema>,
225 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
226 L<Catalyst::Helper::Model::DBIC::SchemaLoader>
230 Brandon L Black, C<blblack@gmail.com>
234 This program is free software, you can redistribute it and/or modify it
235 under the same terms as Perl itself.