1 package Catalyst::Model::DBIC::Schema;
8 use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
10 use UNIVERSAL::require;
15 __PACKAGE__->mk_classaccessor('composed_schema');
16 __PACKAGE__->mk_accessors('schema');
20 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
24 Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema:
30 Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm:
32 package MyApp::Schema::FilmDB;
33 use base qw/DBIx::Class::Schema/;
35 __PACKAGE__->load_classes(qw/Actor Role/);
39 Create some classes for the tables in the database, for example an
40 Actor in MyApp/Schema/FilmDB/Actor.pm:
42 package MyApp::Schema::FilmDB::Actor;
43 use base qw/DBIx::Class/
45 __PACKAGE__->load_components(qw/Core/);
46 __PACKAGE__->table('actor');
50 and a Role in MyApp/Schema/FilmDB/Role.pm:
52 package MyApp::Schema::FilmDB::Role;
53 use base qw/DBIx::Class/
55 __PACKAGE__->load_components(qw/Core/);
56 __PACKAGE__->table('role');
60 Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's
61 usable as a standalone module and you can test/run it without Catalyst.
65 To expose it to Catalyst as a model, you should create a DBIC Model in
66 MyApp/Model/FilmDB.pm:
68 package MyApp::Model::FilmDB;
69 use base qw/Catalyst::Model::DBIC::Schema/;
72 schema_class => 'MyApp::Schema::FilmDB',
81 See below for a full list of the possible config parameters.
85 Now you have a working Model, accessing your separate DBIC Schema. Which can
86 be used/accessed in the normal Catalyst manner, via $c->model():
88 my $actor = $c->model('FilmDB::Actor')->find(1);
90 You can also use it to set up DBIC authentication with
91 Authentication::Store::DBIC in MyApp.pm:
95 use Catalyst qw/... Authentication::Store::DBIC/;
99 __PACKAGE__->config->{authentication}{dbic} = {
100 user_class => 'FilmDB::Actor',
101 user_field => 'name',
102 password_field => 'password'
105 C<< $c->model('Schema::Source') >> returns a L<DBIx::Class::ResultSet> for
106 the source name parameter passed. To find out more about which methods can
107 be called on a ResultSet, or how to add your own methods to it, please see
108 the ResultSet documentation in the L<DBIx::Class> distribution.
110 Some examples are given below:
112 # to access schema methods directly:
113 $c->model('FilmDB')->schema->source(...);
115 # to access the source object, resultset, and class:
116 $c->model('FilmDB')->source(...);
117 $c->model('FilmDB')->resultset(...);
118 $c->model('FilmDB')->class(...);
120 # For resultsets, there's an even quicker shortcut:
121 $c->model('FilmDB::Actor')
122 # is the same as $c->model('FilmDB')->resultset('Actor')
124 # To get the composed schema for making new connections:
125 my $newconn = $c->model('FilmDB')->composed_schema->connect(...);
127 # Or the same thing via a convenience shortcut:
128 my $newconn = $c->model('FilmDB')->connect(...);
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(...);
135 # and again, a convenience shortcut
136 my $newconn = $c->model('FilmDB')->clone();
137 $newconn->storage_type('::LDAP');
138 $newconn->connection(...);
142 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
143 the documentation for L<Catalyst::Helper::Model::DBIC::Schema> for
144 information on generating these Models via Helper scripts.
146 When your Catalyst app starts up, a thin Model layer is created as an
147 interface to your DBIC Schema. It should be clearly noted that the model
148 object returned by C<< $c->model('FilmDB') >> is NOT itself a DBIC schema or
149 resultset object, but merely a wrapper proving L<methods|/METHODS> to access
150 the underlying schema.
152 In addition to this model class, a shortcut class is generated for each
153 source in the schema, allowing easy and direct access to a resultset of the
154 corresponding type. These generated classes are even thinner than the model
155 class, providing no public methods but simply hooking into Catalyst's
156 model() accessor via the
157 L<ACCEPT_CONTEXT|Catalyst::Component/ACCEPT_CONTEXT> mechanism. The complete
158 contents of each generated class is roughly equivalent to the following:
160 package MyApp::Model::FilmDB::Actor
163 $c->model('FilmDB')->resultset('Actor');
166 In short, there are three techniques available for obtaining a DBIC
170 my $rs = $c->model('FilmDB')->schema->resultset('Actor');
172 # using the shortcut method on the model object
173 my $rs = $c->model('FilmDB')->resultset('Actor');
175 # using the generated class directly
176 my $rs = $c->model('FilmDB::Actor');
178 =head1 CONFIG PARAMETERS
184 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
185 to be findable in C<@INC>, but it does not need to be inside the
186 C<Catalyst::Model::> namespace. This parameter is required.
190 This is an arrayref of connection parameters, which are specific to your
191 C<storage_type> (see your storage type documentation for more details).
192 If you only need one parameter (e.g. the DSN), you can just pass a string
193 instead of an arrayref.
195 This is not required if C<schema_class> already has connection information
196 defined inside itself (which isn't highly recommended, but can be done)
198 For L<DBIx::Class::Storage::DBI>, which is the only supported
199 C<storage_type> in L<DBIx::Class> at the time of this writing, the
200 parameters are your dsn, username, password, and connect options hashref.
202 See L<DBIx::Class::Storage::DBI/connect_info> for a detailed explanation
203 of the arguments supported.
207 connect_info => [ 'dbi:Pg:dbname=mypgdb', 'postgres', '' ],
210 'dbi:SQLite:dbname=foo.db',
213 'PRAGMA synchronous = OFF',
219 'dbi:Pg:dbname=mypgdb',
225 'some SQL statement',
226 'another SQL statement',
231 Or using L<Config::General>:
234 schema_class MyApp::Schema::FilmDB
235 connect_info dbi:Pg:dbname=mypgdb
236 connect_info postgres
240 on_connect_do some SQL statement
241 on_connect_do another SQL statement
248 schema_class MyApp::Schema::FilmDB
249 connect_info dbi:SQLite:dbname=foo.db
255 Allows the use of a different C<storage_type> than what is set in your
256 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
257 L<DBIx::Class>). Completely optional, and probably unnecessary for most
258 people until other storage backends become available for L<DBIx::Class>.
268 Instantiates the Model based on the above-documented ->config parameters.
269 The only required parameter is C<schema_class>. C<connect_info> is
270 required in the case that C<schema_class> does not already have connection
271 information defined for it.
275 Accessor which returns the connected schema being used by the this model.
276 There are direct shortcuts on the model class itself for
277 schema->resultset, schema->source, and schema->class.
279 =item composed_schema
281 Accessor which returns the composed schema, which has no connection info,
282 which was used in constructing the C<schema> above. Useful for creating
283 new connections based on the same schema/model. There are direct shortcuts
284 from the model object for composed_schema->clone and composed_schema->connect
288 Shortcut for ->composed_schema->clone
292 Shortcut for ->composed_schema->connect
296 Shortcut for ->schema->source
300 Shortcut for ->schema->class
304 Shortcut for ->schema->resultset
308 Provides an accessor for the connected schema's storage object.
309 Used often for debugging and controlling transactions.
316 my $self = shift->NEXT::new(@_);
318 my $class = ref($self);
319 my $model_name = $class;
320 $model_name =~ s/^[\w:]+::(?:Model|M):://;
322 croak "->config->{schema_class} must be defined for this model"
323 unless $self->{schema_class};
325 my $schema_class = $self->{schema_class};
327 $schema_class->require
328 or croak "Cannot load schema class '$schema_class': $@";
330 if( !$self->{connect_info} ) {
331 if($schema_class->storage && $schema_class->storage->connect_info) {
332 $self->{connect_info} = $schema_class->storage->connect_info;
335 croak "Either ->config->{connect_info} must be defined for $class"
336 . " or $schema_class must have connect info defined on it."
337 . " Here's what we got:\n"
342 $self->composed_schema($schema_class->compose_namespace($class));
343 $self->schema($self->composed_schema->clone);
345 $self->schema->storage_type($self->{storage_type})
346 if $self->{storage_type};
348 $self->schema->connection(
349 ref $self->{connect_info} eq 'ARRAY' ?
350 @{$self->{connect_info}} :
351 $self->{connect_info}
355 foreach my $moniker ($self->schema->sources) {
356 my $classname = "${class}::$moniker";
357 *{"${classname}::ACCEPT_CONTEXT"} = sub {
359 shift->model($model_name)->resultset($moniker);
366 sub clone { shift->composed_schema->clone(@_); }
368 sub connect { shift->composed_schema->connect(@_); }
370 sub storage { shift->schema->storage(@_); }
374 General Catalyst Stuff:
376 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
377 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
379 Stuff related to DBIC and this Model style:
381 L<DBIx::Class>, L<DBIx::Class::Schema>,
382 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>
386 Brandon L Black, C<blblack@gmail.com>
390 This program is free software, you can redistribute it and/or modify it
391 under the same terms as Perl itself.