1 package Catalyst::Model::DBIC::Schema;
4 use base qw/Catalyst::Model Class::Accessor::Fast Class::Data::Accessor/;
6 use UNIVERSAL::require;
11 our $VERSION = '0.16';
13 __PACKAGE__->mk_classaccessor('composed_schema');
14 __PACKAGE__->mk_accessors('schema');
18 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
22 Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema:
28 Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm:
30 package MyApp::Schema::FilmDB;
31 use base qw/DBIx::Class::Schema/;
33 __PACKAGE__->load_classes(qw/Actor Role/);
37 Create some classes for the tables in the database, for example an
38 Actor in MyApp/Schema/FilmDB/Actor.pm:
40 package MyApp::Schema::FilmDB::Actor;
41 use base qw/DBIx::Class/
43 __PACKAGE__->load_components(qw/Core/);
44 __PACKAGE__->table('actor');
48 and a Role in MyApp/Schema/Role.pm:
50 package MyApp::Schema::FilmDB::Role;
51 use base qw/DBIx::Class/
53 __PACKAGE__->load_components(qw/Core/);
54 __PACKAGE__->table('role');
58 Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's
59 usable as a standalone module and you can test/run it without Catalyst.
63 To expose it to Catalyst as a model, you should create a DBIC Model in
64 MyApp/Model/FilmDB.pm:
66 package MyApp::Model::FilmDB;
67 use base qw/Catalyst::Model::DBIC::Schema/;
70 schema_class => 'MyApp::Schema::FilmDB',
79 See below for a full list of the possible config parameters.
83 Now you have a working Model, accessing your separate DBIC Schema. Which can
84 be used/accessed in the normal Catalyst manner, via $c->model():
86 my $actor = $c->model('FilmDB::Actor')->find(1);
88 You can also use it to set up DBIC authentication with
89 Authentication::Store::DBIC in MyApp.pm:
93 use Catalyst qw/... Authentication::Store::DBIC/;
97 __PACKAGE__->config->{authentication}{dbic} = {
98 user_class => 'FilmDB::Actor',
100 password_field => 'password'
103 C<< $c->model() >> returns a L<DBIx::Class::ResultSet> for the source name
104 parameter passed. To find out more about which methods can be called on a
105 ResultSet, or how to add your own methods to it, please see the ResultSet
106 documentation in the L<DBIx::Class> distribution.
108 Some examples are given below:
110 # to access schema methods directly:
111 $c->model('FilmDB')->schema->source(...);
113 # to access the source object, resultset, and class:
114 $c->model('FilmDB')->source(...);
115 $c->model('FilmDB')->resultset(...);
116 $c->model('FilmDB')->class(...);
118 # For resultsets, there's an even quicker shortcut:
119 $c->model('FilmDB::Actor')
120 # is the same as $c->model('FilmDB')->resultset('Actor')
122 # To get the composed schema for making new connections:
123 my $newconn = $c->model('FilmDB')->composed_schema->connect(...);
125 # Or the same thing via a convenience shortcut:
126 my $newconn = $c->model('FilmDB')->connect(...);
128 # or, if your schema works on different storage drivers:
129 my $newconn = $c->model('FilmDB')->composed_schema->clone();
130 $newconn->storage_type('::LDAP');
131 $newconn->connection(...);
133 # and again, a convenience shortcut
134 my $newconn = $c->model('FilmDB')->clone();
135 $newconn->storage_type('::LDAP');
136 $newconn->connection(...);
140 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
141 the documentation for L<Catalyst::Helper::Model::DBIC::Schema> for
142 information on generating these Models via Helper scripts.
144 =head1 CONFIG PARAMETERS
150 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
151 to be findable in C<@INC>, but it does not need to be inside the
152 C<Catalyst::Model::> namespace. This parameter is required.
156 This is an arrayref of connection parameters, which are specific to your
157 C<storage_type> (see your storage type documentation for more details).
159 This is not required if C<schema_class> already has connection information
160 defined inside itself (which isn't highly recommended, but can be done)
162 For L<DBIx::Class::Storage::DBI>, which is the only supported
163 C<storage_type> in L<DBIx::Class> at the time of this writing, the
164 parameters are your dsn, username, password, and connect options hashref.
166 If you need to specify the L<DBIx::Class::Storage::DBI> specific parameter
167 C<on_connect_do>, or the related C<sql_maker> options C<limit_dialect>,
168 C<quote_char>, or C<name_sep>, you can place these options into a hashref
169 as the final element of the C<connect_info> arrayref. If in doubt, don't
170 specify these options. You would know it if you needed them.
174 connect_info => [ 'dbi:Pg:dbname=mypgdb', 'postgres', '' ],
177 'dbi:SQLite:dbname=foo.db',
180 'PRAGMA synchronous = OFF',
186 'dbi:Pg:dbname=mypgdb',
192 'some SQL statement',
193 'another SQL statement',
200 Allows the use of a different C<storage_type> than what is set in your
201 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
202 L<DBIx::Class>). Completely optional, and probably unnecessary for most
203 people until other storage backends become available for L<DBIx::Class>.
213 Instantiates the Model based on the above-documented ->config parameters.
214 The only required parameter is C<schema_class>. C<connect_info> is
215 required in the case that C<schema_class> does not already have connection
216 information defined for it.
220 Accessor which returns the connected schema being used by the this model.
221 There are direct shortcuts on the model class itself for
222 schema->resultset, schema->source, and schema->class.
224 =item composed_schema
226 Accessor which returns the composed schema, which has no connection info,
227 which was used in constructing the C<schema> above. Useful for creating
228 new connections based on the same schema/model. There are direct shortcuts
229 from the model object for composed_schema->clone and composed_schema->connect
233 Shortcut for ->composed_schema->clone
237 Shortcut for ->composed_schema->connect
241 Shortcut for ->schema->source
245 Shortcut for ->schema->class
249 Shortcut for ->schema->resultset
253 Provides an accessor for the connected schema's storage object.
254 Used often for debugging and controlling transactions.
261 my $self = shift->NEXT::new(@_);
263 my $class = ref($self);
264 my $model_name = $class;
265 $model_name =~ s/^[\w:]+::(?:Model|M):://;
267 croak "->config->{schema_class} must be defined for this model"
268 unless $self->{schema_class};
270 my $schema_class = $self->{schema_class};
272 $schema_class->require
273 or croak "Cannot load schema class '$schema_class': $@";
275 if( !$self->{connect_info} ) {
276 if($schema_class->storage && $schema_class->storage->connect_info) {
277 $self->{connect_info} = $schema_class->storage->connect_info;
280 croak "Either ->config->{connect_info} must be defined for $class"
281 . " or $schema_class must have connect info defined on it"
282 . "Here's what we got:\n"
287 $self->composed_schema($schema_class->compose_namespace($class));
288 $self->schema($self->composed_schema->clone);
290 $self->schema->storage_type($self->{storage_type})
291 if $self->{storage_type};
293 # XXX This is temporary, until DBIx::Class::Storage::DBI supports the
294 # same syntax and we switch our requisite to that version somewhere
295 # down the line. This syntax is already committed into DBIx::Class
296 # -current branch post-0.06.
297 # At that time, this whole block can revert back to just being:
298 # $self->schema->connection(@{$self->{connect_info}});
300 my $connect_info = [ @{$self->{connect_info}} ];
301 my ($on_connect_do, %sql_maker_opts);
302 if($DBIx::Class::VERSION < 0.069) {
304 my $last_info = $self->{connect_info}->[-1];
305 if(ref $last_info eq 'HASH') {
306 if($on_connect_do = $last_info->{on_connect_do}) {
309 for my $sql_maker_opt (qw/limit_dialect quote_char name_sep/) {
310 if(my $opt_val = $last_info->{$sql_maker_opt}) {
312 $sql_maker_opts{$sql_maker_opt} = $opt_val;
315 pop(@$connect_info) if $used;
319 $self->schema->connection(@$connect_info);
321 if($DBIx::Class::VERSION < 0.069) {
322 $self->schema->storage->on_connect_do($on_connect_do)
324 foreach my $sql_maker_opt (keys %sql_maker_opts) {
325 $self->schema->storage->sql_maker->$sql_maker_opt(
326 $sql_maker_opts{$sql_maker_opt}
331 # XXX end of compatibility block referenced above
334 foreach my $moniker ($self->schema->sources) {
335 my $classname = "${class}::$moniker";
336 *{"${classname}::ACCEPT_CONTEXT"} = sub {
338 shift->model($model_name)->resultset($moniker);
345 sub clone { shift->composed_schema->clone(@_); }
347 sub connect { shift->composed_schema->connect(@_); }
349 sub storage { shift->schema->storage(@_); }
353 General Catalyst Stuff:
355 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
356 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
358 Stuff related to DBIC and this Model style:
360 L<DBIx::Class>, L<DBIx::Class::Schema>,
361 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>
365 Brandon L Black, C<blblack@gmail.com>
369 This program is free software, you can redistribute it and/or modify it
370 under the same terms as Perl itself.