1 package Catalyst::Model::DBIC::Schema;
4 use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
6 use UNIVERSAL::require;
7 use UNIVERSAL qw/ can /;
10 our $VERSION = '0.03';
12 __PACKAGE__->mk_classaccessor('composed_schema');
13 __PACKAGE__->mk_accessors('schema');
17 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
21 package MyApp::Model::Foo;
23 use base 'Catalyst::Model::DBIC::Schema';
26 schema_class => 'Foo::SchemaClass',
27 connect_info => [ 'dbi:Pg:dbname=foodb',
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::SchemaInlineLoader> for information
71 on generating these Models via Helper scripts.
73 =head1 CONFIG PARAMETERS
79 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
80 to be findable in C<@INC>, but it does not need to be underneath
81 C<Catalyst::Model::>. This parameter is required.
85 This is an arrayref of connection parameters, which are specific to your
86 C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
87 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
88 dsn, username, password, and connect options hashref.
90 This is not required if C<schema_class> already has connection information
91 defined in itself (which would be the case for a Schema defined by
92 L<DBIx::Class::Schema::Loader>, for instance).
96 Allows the use of a different C<storage_type> than what is set in your
97 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
98 L<DBIx::Class>). Completely optional, and probably unneccesary for most
99 people, until other storage backends become available for L<DBIx::Class>.
109 Instantiates the Model based on the above-documented ->config parameters.
110 The only required parameter is C<schema_class>. C<connect_info> is
111 required in the case that C<schema_class> does not already have connection
112 information defined for it.
116 Accessor which returns the connected schema being used by the this model.
117 There are already direct shortcuts on the model class itself for
118 schema->resultset, schema->source, and schema->class.
120 =item composed_schema
122 Accessor which returns the composed schema, which has no connection info,
123 which was used in constructing the C<schema> above. Useful for creating
124 new connections based on the same schema/model. There are direct shortcuts
125 from the model object for composed_schema->clone and composed_schema->connect
129 Shortcut for ->composed_schema->clone
133 Shortcut for ->composed_schema->connect
137 Shortcut for ->schema->source
141 Shortcut for ->schema->class
145 Shortcut for ->schema->resultset
152 my $self = shift->NEXT::new(@_);
154 my $class = ref($self);
155 my $model_name = $class;
156 $model_name =~ s/^[\w:]+::(?:Model|M):://;
158 croak "->config->{schema_class} must be defined for this model"
159 unless $self->{schema_class};
161 my $schema_class = $self->{schema_class};
163 # don't require if the class is already loaded...
164 if( !can($schema_class, 'source') ) {
165 $schema_class->require
166 or croak "Cannot load schema class '$schema_class': $@";
169 if( !$self->{connect_info} ) {
170 if($schema_class->storage && $schema_class->storage->connect_info) {
171 $self->{connect_info} = $schema_class->storage->connect_info;
174 croak "Either ->config->{connect_info} must be defined for $class"
175 . " or $schema_class must have connection defined on it";
179 $self->composed_schema($schema_class->compose_namespace($class));
180 $self->schema($self->composed_schema->clone);
181 $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
182 $self->schema->connection(@{$self->{connect_info}});
185 foreach my $moniker ($self->schema->sources) {
186 *{"${class}::${moniker}::ACCEPT_CONTEXT"} = sub {
188 shift->model($model_name)->resultset($moniker);
195 sub clone { shift->composed_schema->clone(@_); }
197 sub connect { shift->composed_schema->connect(@_); }
201 General Catalyst Stuff:
203 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
204 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
206 Stuff related to DBIC and this Model style:
208 L<DBIx::Class>, L<DBIx::Class::Schema>,
209 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>,
210 L<Catalyst::Helper::Model::DBIC::SchemaInlineLoader>
214 Brandon L Black, C<blblack@gmail.com>
218 This program is free software, you can redistribute it and/or modify it
219 under the same terms as Perl itself.