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 NOTE: This is the first public release, there's probably a higher than
68 average chance of random bugs and shortcomings: you've been warned.
70 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
71 the documentation for L<Catalyst::Helper::Model::DBIC::Schema> and
72 L<Catalyst::Helper::Model::DBIC::SchemaLoader> for information
73 on generating these Models via Helper scripts. The latter of the two
74 will also generated a L<DBIx::Class::Schema::Loader>-based Schema class
77 =head1 CONFIG PARAMETERS
83 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
84 to be findable in C<@INC>, but it does not need to be underneath
85 C<Catalyst::Model::>. This parameter is required.
89 This is an arrayref of connection parameters, which are specific to your
90 C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
91 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
92 dsn, username, password, and connect options hashref.
94 This is not required if C<schema_class> already has connection information
95 defined in itself (which would be the case for a Schema defined by
96 L<DBIx::Class::Schema::Loader>, for instance).
100 Allows the use of a different C<storage_type> than what is set in your
101 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
102 L<DBIx::Class>). Completely optional, and probably unneccesary for most
103 people, until other storage backends become available for L<DBIx::Class>.
113 Instantiates the Model based on the above-documented ->config parameters.
114 The only required parameter is C<schema_class>. C<connect_info> is
115 required in the case that C<schema_class> does not already have connection
116 information defined for it.
120 Accessor which returns the connected schema being used by the this model.
121 There are already direct shortcuts on the model class itself for
122 schema->resultset, schema->source, and schema->class.
124 =item composed_schema
126 Accessor which returns the composed schema, which has no connection info,
127 which was used in constructing the C<schema> above. Useful for creating
128 new connections based on the same schema/model. There are direct shortcuts
129 from the model object for composed_schema->clone and composed_schema->connect
133 Shortcut for ->composed_schema->clone
137 Shortcut for ->composed_schema->connect
141 Shortcut for ->schema->source
145 Shortcut for ->schema->class
149 Shortcut for ->schema->resultset
156 my $self = shift->NEXT::new(@_);
158 my $class = ref($self);
159 my $model_name = $class;
160 $model_name =~ s/^[\w:]+::(?:Model|M):://;
162 croak "->config->{schema_class} must be defined for this model"
163 unless $self->{schema_class};
165 my $schema_class = $self->{schema_class};
167 $schema_class->require
168 or croak "Cannot load schema class '$schema_class': $@";
170 if( !$self->{connect_info} ) {
171 if($schema_class->storage && $schema_class->storage->connect_info) {
172 $self->{connect_info} = $schema_class->storage->connect_info;
175 croak "Either ->config->{connect_info} must be defined for $class"
176 . " or $schema_class must have connect info defined on it";
180 $self->composed_schema($schema_class->compose_namespace($class));
181 $self->schema($self->composed_schema->clone);
182 $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
183 $self->schema->connection(@{$self->{connect_info}});
186 foreach my $moniker ($self->schema->sources) {
187 my $classname = "${class}::$moniker";
188 *{"${classname}::ACCEPT_CONTEXT"} = sub {
190 shift->model($model_name)->resultset($moniker);
197 sub clone { shift->composed_schema->clone(@_); }
199 sub connect { shift->composed_schema->connect(@_); }
203 General Catalyst Stuff:
205 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
206 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
208 Stuff related to DBIC and this Model style:
210 L<DBIx::Class>, L<DBIx::Class::Schema>,
211 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
212 L<Catalyst::Helper::Model::DBIC::SchemaLoader>
216 Brandon L Black, C<blblack@gmail.com>
220 This program is free software, you can redistribute it and/or modify it
221 under the same terms as Perl itself.