1 package Catalyst::Model::DBIC::Schema;
8 use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
12 use UNIVERSAL::require;
17 __PACKAGE__->mk_classaccessor('composed_schema');
18 __PACKAGE__->mk_accessors('schema');
22 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
26 Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema:
32 Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm:
34 package MyApp::Schema::FilmDB;
35 use base qw/DBIx::Class::Schema/;
37 __PACKAGE__->load_classes(qw/Actor Role/);
41 Create some classes for the tables in the database, for example an
42 Actor in MyApp/Schema/FilmDB/Actor.pm:
44 package MyApp::Schema::FilmDB::Actor;
45 use base qw/DBIx::Class/
47 __PACKAGE__->load_components(qw/Core/);
48 __PACKAGE__->table('actor');
52 and a Role in MyApp/Schema/FilmDB/Role.pm:
54 package MyApp::Schema::FilmDB::Role;
55 use base qw/DBIx::Class/
57 __PACKAGE__->load_components(qw/Core/);
58 __PACKAGE__->table('role');
62 Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's
63 usable as a standalone module and you can test/run it without Catalyst.
67 To expose it to Catalyst as a model, you should create a DBIC Model in
68 MyApp/Model/FilmDB.pm:
70 package MyApp::Model::FilmDB;
71 use base qw/Catalyst::Model::DBIC::Schema/;
74 schema_class => 'MyApp::Schema::FilmDB',
83 See below for a full list of the possible config parameters.
87 Now you have a working Model which accesses your separate DBIC Schema. This can
88 be used/accessed in the normal Catalyst manner, via $c->model():
90 my $actor = $c->model('FilmDB::Actor')->find(1);
92 You can also use it to set up DBIC authentication with
93 Authentication::Store::DBIC in MyApp.pm:
97 use Catalyst qw/... Authentication::Store::DBIC/;
101 __PACKAGE__->config->{authentication}{dbic} = {
102 user_class => 'FilmDB::Actor',
103 user_field => 'name',
104 password_field => 'password'
107 C<< $c->model('Schema::Source') >> returns a L<DBIx::Class::ResultSet> for
108 the source name parameter passed. To find out more about which methods can
109 be called on a ResultSet, or how to add your own methods to it, please see
110 the ResultSet documentation in the L<DBIx::Class> distribution.
112 Some examples are given below:
114 # to access schema methods directly:
115 $c->model('FilmDB')->schema->source(...);
117 # to access the source object, resultset, and class:
118 $c->model('FilmDB')->source(...);
119 $c->model('FilmDB')->resultset(...);
120 $c->model('FilmDB')->class(...);
122 # For resultsets, there's an even quicker shortcut:
123 $c->model('FilmDB::Actor')
124 # is the same as $c->model('FilmDB')->resultset('Actor')
126 # To get the composed schema for making new connections:
127 my $newconn = $c->model('FilmDB')->composed_schema->connect(...);
129 # Or the same thing via a convenience shortcut:
130 my $newconn = $c->model('FilmDB')->connect(...);
132 # or, if your schema works on different storage drivers:
133 my $newconn = $c->model('FilmDB')->composed_schema->clone();
134 $newconn->storage_type('::LDAP');
135 $newconn->connection(...);
137 # and again, a convenience shortcut
138 my $newconn = $c->model('FilmDB')->clone();
139 $newconn->storage_type('::LDAP');
140 $newconn->connection(...);
144 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
145 the documentation for L<Catalyst::Helper::Model::DBIC::Schema> for
146 information on generating these Models via Helper scripts.
148 When your Catalyst app starts up, a thin Model layer is created as an
149 interface to your DBIC Schema. It should be clearly noted that the model
150 object returned by C<< $c->model('FilmDB') >> is NOT itself a DBIC schema or
151 resultset object, but merely a wrapper proving L<methods|/METHODS> to access
152 the underlying schema.
154 In addition to this model class, a shortcut class is generated for each
155 source in the schema, allowing easy and direct access to a resultset of the
156 corresponding type. These generated classes are even thinner than the model
157 class, providing no public methods but simply hooking into Catalyst's
158 model() accessor via the
159 L<ACCEPT_CONTEXT|Catalyst::Component/ACCEPT_CONTEXT> mechanism. The complete
160 contents of each generated class is roughly equivalent to the following:
162 package MyApp::Model::FilmDB::Actor
165 $c->model('FilmDB')->resultset('Actor');
168 In short, there are three techniques available for obtaining a DBIC
172 my $rs = $c->model('FilmDB')->schema->resultset('Actor');
174 # using the shortcut method on the model object
175 my $rs = $c->model('FilmDB')->resultset('Actor');
177 # using the generated class directly
178 my $rs = $c->model('FilmDB::Actor');
180 In order to add methods to a DBIC resultset, you cannot simply add them to
181 the source (row, table) definition class; you must define a separate custom
182 resultset class. See L<DBIx::Class::Manual::Cookbook/"Predefined searches">
185 =head1 CONFIG PARAMETERS
191 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
192 to be findable in C<@INC>, but it does not need to be inside the
193 C<Catalyst::Model::> namespace. This parameter is required.
197 This is an arrayref of connection parameters, which are specific to your
198 C<storage_type> (see your storage type documentation for more details).
199 If you only need one parameter (e.g. the DSN), you can just pass a string
200 instead of an arrayref.
202 This is not required if C<schema_class> already has connection information
203 defined inside itself (which isn't highly recommended, but can be done)
205 For L<DBIx::Class::Storage::DBI>, which is the only supported
206 C<storage_type> in L<DBIx::Class> at the time of this writing, the
207 parameters are your dsn, username, password, and connect options hashref.
209 See L<DBIx::Class::Storage::DBI/connect_info> for a detailed explanation
210 of the arguments supported.
214 connect_info => [ 'dbi:Pg:dbname=mypgdb', 'postgres', '' ],
217 'dbi:SQLite:dbname=foo.db',
220 'PRAGMA synchronous = OFF',
226 'dbi:Pg:dbname=mypgdb',
232 'some SQL statement',
233 'another SQL statement',
238 Or using L<Config::General>:
241 schema_class MyApp::Schema::FilmDB
242 connect_info dbi:Pg:dbname=mypgdb
243 connect_info postgres
247 on_connect_do some SQL statement
248 on_connect_do another SQL statement
255 schema_class MyApp::Schema::FilmDB
256 connect_info dbi:SQLite:dbname=foo.db
262 Allows the use of a different C<storage_type> than what is set in your
263 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
264 L<DBIx::Class>). Completely optional, and probably unnecessary for most
265 people until other storage backends become available for L<DBIx::Class>.
275 Instantiates the Model based on the above-documented ->config parameters.
276 The only required parameter is C<schema_class>. C<connect_info> is
277 required in the case that C<schema_class> does not already have connection
278 information defined for it.
282 Accessor which returns the connected schema being used by the this model.
283 There are direct shortcuts on the model class itself for
284 schema->resultset, schema->source, and schema->class.
286 =item composed_schema
288 Accessor which returns the composed schema, which has no connection info,
289 which was used in constructing the C<schema> above. Useful for creating
290 new connections based on the same schema/model. There are direct shortcuts
291 from the model object for composed_schema->clone and composed_schema->connect
295 Shortcut for ->composed_schema->clone
299 Shortcut for ->composed_schema->connect
303 Shortcut for ->schema->source
307 Shortcut for ->schema->class
311 Shortcut for ->schema->resultset
315 Provides an accessor for the connected schema's storage object.
316 Used often for debugging and controlling transactions.
323 my $self = shift->next::method(@_);
325 my $class = ref($self);
326 my $model_name = $class;
327 $model_name =~ s/^[\w:]+::(?:Model|M):://;
329 croak "->config->{schema_class} must be defined for this model"
330 unless $self->{schema_class};
332 my $schema_class = $self->{schema_class};
334 $schema_class->require
335 or croak "Cannot load schema class '$schema_class': $@";
337 if( !$self->{connect_info} ) {
338 if($schema_class->storage && $schema_class->storage->connect_info) {
339 $self->{connect_info} = $schema_class->storage->connect_info;
342 croak "Either ->config->{connect_info} must be defined for $class"
343 . " or $schema_class must have connect info defined on it."
344 . " Here's what we got:\n"
349 $self->composed_schema($schema_class->compose_namespace($class));
350 $self->schema($self->composed_schema->clone);
352 $self->schema->storage_type($self->{storage_type})
353 if $self->{storage_type};
355 $self->schema->connection(
356 ref $self->{connect_info} eq 'ARRAY' ?
357 @{$self->{connect_info}} :
358 $self->{connect_info}
362 foreach my $moniker ($self->schema->sources) {
363 my $classname = "${class}::$moniker";
364 *{"${classname}::ACCEPT_CONTEXT"} = sub {
366 shift->model($model_name)->resultset($moniker);
373 sub clone { shift->composed_schema->clone(@_); }
375 sub connect { shift->composed_schema->connect(@_); }
377 sub storage { shift->schema->storage(@_); }
381 General Catalyst Stuff:
383 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
384 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
386 Stuff related to DBIC and this Model style:
388 L<DBIx::Class>, L<DBIx::Class::Schema>,
389 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>
393 Brandon L Black, C<blblack@gmail.com>
397 This program is free software, you can redistribute it and/or modify it
398 under the same terms as Perl itself.