Doc patch to clarify generated classes
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema.pm
CommitLineData
ad91060a 1package Catalyst::Model::DBIC::Schema;
2
3use strict;
018eb0e2 4use warnings;
5
460e3ac8 6our $VERSION = '0.22';
018eb0e2 7
f1613faa 8use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
ad91060a 9use NEXT;
10use UNIVERSAL::require;
11use Carp;
bfcd6e3d 12use Data::Dumper;
7db6da78 13require DBIx::Class;
ad91060a 14
f1613faa 15__PACKAGE__->mk_classaccessor('composed_schema');
16__PACKAGE__->mk_accessors('schema');
17
ad91060a 18=head1 NAME
19
20Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
21
22=head1 SYNOPSIS
23
aabc1d75 24Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema:
25
26=over
27
28=item 1.
29
30Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm:
31
32 package MyApp::Schema::FilmDB;
33 use base qw/DBIx::Class::Schema/;
34
35 __PACKAGE__->load_classes(qw/Actor Role/);
36
37=item 2.
38
39Create some classes for the tables in the database, for example an
40Actor in MyApp/Schema/FilmDB/Actor.pm:
41
42 package MyApp::Schema::FilmDB::Actor;
43 use base qw/DBIx::Class/
07edc53e 44
aabc1d75 45 __PACKAGE__->load_components(qw/Core/);
46 __PACKAGE__->table('actor');
07edc53e 47
aabc1d75 48 ...
49
c8ae74f8 50and a Role in MyApp/Schema/FilmDB/Role.pm:
aabc1d75 51
52 package MyApp::Schema::FilmDB::Role;
53 use base qw/DBIx::Class/
07edc53e 54
aabc1d75 55 __PACKAGE__->load_components(qw/Core/);
4a3e80e9 56 __PACKAGE__->table('role');
07edc53e 57
aabc1d75 58 ...
59
60Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's
61usable as a standalone module and you can test/run it without Catalyst.
62
63=item 3.
64
65To expose it to Catalyst as a model, you should create a DBIC Model in
66MyApp/Model/FilmDB.pm:
67
68 package MyApp::Model::FilmDB;
69 use base qw/Catalyst::Model::DBIC::Schema/;
07edc53e 70
aabc1d75 71 __PACKAGE__->config(
72 schema_class => 'MyApp::Schema::FilmDB',
73 connect_info => [
74 "DBI:...",
75 "username",
76 "password",
77 {AutoCommit => 1}
78 ]
79 );
80
81See below for a full list of the possible config parameters.
82
83=back
84
85Now you have a working Model, accessing your separate DBIC Schema. Which can
86be used/accessed in the normal Catalyst manner, via $c->model():
87
88 my $actor = $c->model('FilmDB::Actor')->find(1);
89
90You can also use it to set up DBIC authentication with
91Authentication::Store::DBIC in MyApp.pm:
92
93 package MyApp;
07edc53e 94
aabc1d75 95 use Catalyst qw/... Authentication::Store::DBIC/;
07edc53e 96
aabc1d75 97 ...
07edc53e 98
aabc1d75 99 __PACKAGE__->config->{authentication}{dbic} = {
100 user_class => 'FilmDB::Actor',
101 user_field => 'name',
102 password_field => 'password'
103 }
104
d52bc376 105C<< $c->model('Schema::Source') >> returns a L<DBIx::Class::ResultSet> for
106the source name parameter passed. To find out more about which methods can
107be called on a ResultSet, or how to add your own methods to it, please see
108the ResultSet documentation in the L<DBIx::Class> distribution.
aabc1d75 109
110Some examples are given below:
111
f1613faa 112 # to access schema methods directly:
113 $c->model('FilmDB')->schema->source(...);
114
115 # to access the source object, resultset, and class:
07edc53e 116 $c->model('FilmDB')->source(...);
117 $c->model('FilmDB')->resultset(...);
118 $c->model('FilmDB')->class(...);
c12b7310 119
07edc53e 120 # For resultsets, there's an even quicker shortcut:
121 $c->model('FilmDB::Actor')
122 # is the same as $c->model('FilmDB')->resultset('Actor')
ad91060a 123
f1613faa 124 # To get the composed schema for making new connections:
125 my $newconn = $c->model('FilmDB')->composed_schema->connect(...);
126
127 # Or the same thing via a convenience shortcut:
128 my $newconn = $c->model('FilmDB')->connect(...);
129
130 # or, if your schema works on different storage drivers:
131 my $newconn = $c->model('FilmDB')->composed_schema->clone();
132 $newconn->storage_type('::LDAP');
133 $newconn->connection(...);
134
135 # and again, a convenience shortcut
136 my $newconn = $c->model('FilmDB')->clone();
137 $newconn->storage_type('::LDAP');
138 $newconn->connection(...);
139
ad91060a 140=head1 DESCRIPTION
141
7b39f3f0 142This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
ef91bcf9 143the documentation for L<Catalyst::Helper::Model::DBIC::Schema> for
144information on generating these Models via Helper scripts.
ad91060a 145
d52bc376 146When your Catalyst app starts up, a thin Model layer is created as an
147interface to your DBIC Schema. It should be clearly noted that the model
148object returned by C<< $c->model('FilmDB') >> is NOT itself a DBIC schema or
149resultset object, but merely a wrapper proving L<methods|/METHODS> to access
150the underlying schema.
151
152In addition to this model class, a shortcut class is generated for each
153source in the schema, allowing easy and direct access to a resultset of the
154corresponding type. These generated classes are even thinner than the model
155class, providing no public methods but simply hooking into Catalyst's
156model() accessor via the
157L<ACCEPT_CONTEXT|Catalyst::Component/ACCEPT_CONTEXT> mechanism. The complete
158contents of each generated class is roughly equivalent to the following:
159
160 package MyApp::Model::FilmDB::Actor
161 sub ACCEPT_CONTEXT {
162 my ($self, $c) = @_;
163 $c->model('FilmDB')->resultset('Actor');
164 }
165
166In short, there are three techniques available for obtaining a DBIC
167resultset object:
168
169 # the long way
170 my $rs = $c->model('FilmDB')->schema->resultset('Actor');
171
172 # using the shortcut method on the model object
173 my $rs = $c->model('FilmDB')->resultset('Actor');
174
175 # using the generated class directly
176 my $rs = $c->model('FilmDB::Actor');
177
ad91060a 178=head1 CONFIG PARAMETERS
179
180=over 4
181
182=item schema_class
183
184This is the classname of your L<DBIx::Class::Schema> Schema. It needs
aabc1d75 185to be findable in C<@INC>, but it does not need to be inside the
186C<Catalyst::Model::> namespace. This parameter is required.
ad91060a 187
188=item connect_info
189
190This is an arrayref of connection parameters, which are specific to your
b9a72351 191C<storage_type> (see your storage type documentation for more details).
192If you only need one parameter (e.g. the DSN), you can just pass a string
193instead of an arrayref.
ad91060a 194
0f2fd2c0 195This is not required if C<schema_class> already has connection information
d89e6c8a 196defined inside itself (which isn't highly recommended, but can be done)
0f2fd2c0 197
7db6da78 198For L<DBIx::Class::Storage::DBI>, which is the only supported
199C<storage_type> in L<DBIx::Class> at the time of this writing, the
200parameters are your dsn, username, password, and connect options hashref.
201
018eb0e2 202See L<DBIx::Class::Storage::DBI/connect_info> for a detailed explanation
203of the arguments supported.
7db6da78 204
205Examples:
206
07edc53e 207 connect_info => [ 'dbi:Pg:dbname=mypgdb', 'postgres', '' ],
208
209 connect_info => [
210 'dbi:SQLite:dbname=foo.db',
211 {
212 on_connect_do => [
213 'PRAGMA synchronous = OFF',
214 ],
215 }
216 ],
217
218 connect_info => [
219 'dbi:Pg:dbname=mypgdb',
220 'postgres',
221 '',
222 { AutoCommit => 0 },
223 {
224 on_connect_do => [
225 'some SQL statement',
226 'another SQL statement',
227 ],
228 }
229 ],
7db6da78 230
8281c933 231Or using L<Config::General>:
232
233 <Model::FilmDB>
234 schema_class MyApp::Schema::FilmDB
235 connect_info dbi:Pg:dbname=mypgdb
236 connect_info postgres
237 connect_info
238 <connect_info>
239 AutoCommit 0
240 on_connect_do some SQL statement
241 on_connect_do another SQL statement
242 </connect_info>
243 </Model::FilmDB>
244
245or
246
247 <Model::FilmDB>
248 schema_class MyApp::Schema::FilmDB
249 connect_info dbi:SQLite:dbname=foo.db
250 </Model::FilmDB>
251
252
ad91060a 253=item storage_type
254
255Allows the use of a different C<storage_type> than what is set in your
256C<schema_class> (which in turn defaults to C<::DBI> if not set in current
f1613faa 257L<DBIx::Class>). Completely optional, and probably unnecessary for most
258people until other storage backends become available for L<DBIx::Class>.
ad91060a 259
260=back
261
262=head1 METHODS
263
264=over 4
265
266=item new
267
268Instantiates the Model based on the above-documented ->config parameters.
0f2fd2c0 269The only required parameter is C<schema_class>. C<connect_info> is
270required in the case that C<schema_class> does not already have connection
271information defined for it.
ad91060a 272
f1613faa 273=item schema
274
275Accessor which returns the connected schema being used by the this model.
276There are direct shortcuts on the model class itself for
277schema->resultset, schema->source, and schema->class.
278
279=item composed_schema
280
281Accessor which returns the composed schema, which has no connection info,
282which was used in constructing the C<schema> above. Useful for creating
283new connections based on the same schema/model. There are direct shortcuts
284from the model object for composed_schema->clone and composed_schema->connect
285
286=item clone
287
288Shortcut for ->composed_schema->clone
289
290=item connect
291
292Shortcut for ->composed_schema->connect
293
294=item source
c12b7310 295
f1613faa 296Shortcut for ->schema->source
297
298=item class
299
300Shortcut for ->schema->class
301
302=item resultset
303
304Shortcut for ->schema->resultset
305
306=item storage
307
308Provides an accessor for the connected schema's storage object.
309Used often for debugging and controlling transactions.
b8427e0b 310
ad91060a 311=back
312
313=cut
314
315sub new {
46878f2e 316 my $self = shift->NEXT::new(@_);
ad91060a 317
318 my $class = ref($self);
319 my $model_name = $class;
320 $model_name =~ s/^[\w:]+::(?:Model|M):://;
321
0f2fd2c0 322 croak "->config->{schema_class} must be defined for this model"
323 unless $self->{schema_class};
ad91060a 324
325 my $schema_class = $self->{schema_class};
326
1aeb6e1e 327 $schema_class->require
f1613faa 328 or croak "Cannot load schema class '$schema_class': $@";
7db6da78 329
f1613faa 330 if( !$self->{connect_info} ) {
331 if($schema_class->storage && $schema_class->storage->connect_info) {
332 $self->{connect_info} = $schema_class->storage->connect_info;
333 }
334 else {
335 croak "Either ->config->{connect_info} must be defined for $class"
460e3ac8 336 . " or $schema_class must have connect info defined on it."
337 . " Here's what we got:\n"
f1613faa 338 . Dumper($self);
339 }
7db6da78 340 }
341
f1613faa 342 $self->composed_schema($schema_class->compose_namespace($class));
343 $self->schema($self->composed_schema->clone);
344
345 $self->schema->storage_type($self->{storage_type})
346 if $self->{storage_type};
7db6da78 347
b9a72351 348 $self->schema->connection(
349 ref $self->{connect_info} eq 'ARRAY' ?
350 @{$self->{connect_info}} :
351 $self->{connect_info}
352 );
f1613faa 353
ad91060a 354 no strict 'refs';
355 foreach my $moniker ($self->schema->sources) {
0b2a7108 356 my $classname = "${class}::$moniker";
7db6da78 357 *{"${classname}::ACCEPT_CONTEXT"} = sub {
ad91060a 358 shift;
c12b7310 359 shift->model($model_name)->resultset($moniker);
ad91060a 360 }
361 }
362
363 return $self;
364}
365
f1613faa 366sub clone { shift->composed_schema->clone(@_); }
367
368sub connect { shift->composed_schema->connect(@_); }
369
370sub storage { shift->schema->storage(@_); }
b8427e0b 371
ad91060a 372=head1 SEE ALSO
373
7b39f3f0 374General Catalyst Stuff:
375
376L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
377L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
378
379Stuff related to DBIC and this Model style:
380
381L<DBIx::Class>, L<DBIx::Class::Schema>,
ef91bcf9 382L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>
ad91060a 383
384=head1 AUTHOR
385
386Brandon L Black, C<blblack@gmail.com>
387
388=head1 COPYRIGHT
389
390This program is free software, you can redistribute it and/or modify it
391under the same terms as Perl itself.
392
393=cut
394
3951;