1 package Catalyst::Model::DBIC::Schema;
4 use base qw/Catalyst::Model/;
6 use UNIVERSAL::require;
11 our $VERSION = '0.17_01';
15 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
19 Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema:
25 Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm:
27 package MyApp::Schema::FilmDB;
28 use base qw/DBIx::Class::Schema/;
30 __PACKAGE__->load_classes(qw/Actor Role/);
34 Create some classes for the tables in the database, for example an
35 Actor in MyApp/Schema/FilmDB/Actor.pm:
37 package MyApp::Schema::FilmDB::Actor;
38 use base qw/DBIx::Class/
40 __PACKAGE__->load_components(qw/Core/);
41 __PACKAGE__->table('actor');
45 and a Role in MyApp/Schema/Role.pm:
47 package MyApp::Schema::FilmDB::Role;
48 use base qw/DBIx::Class/
50 __PACKAGE__->load_components(qw/Core/);
51 __PACKAGE__->table('role');
55 Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's
56 usable as a standalone module and you can test/run it without Catalyst.
60 To expose it to Catalyst as a model, you should create a DBIC Model in
61 MyApp/Model/FilmDB.pm:
63 package MyApp::Model::FilmDB;
64 use base qw/Catalyst::Model::DBIC::Schema/;
67 schema_class => 'MyApp::Schema::FilmDB',
76 See below for a full list of the possible config parameters.
80 Now you have a working Model, accessing your separate DBIC Schema. Which can
81 be used/accessed in the normal Catalyst manner, via $c->model():
83 my $actor = $c->model('FilmDB::Actor')->find(1);
85 You can also use it to set up DBIC authentication with
86 Authentication::Store::DBIC in MyApp.pm:
90 use Catalyst qw/... Authentication::Store::DBIC/;
94 __PACKAGE__->config->{authentication}{dbic} = {
95 user_class => 'FilmDB::Actor',
97 password_field => 'password'
100 C<< $c->model() >> returns a L<DBIx::Class::ResultSet> for the source name
101 parameter passed. To find out more about which methods can be called on a
102 ResultSet, or how to add your own methods to it, please see the ResultSet
103 documentation in the L<DBIx::Class> distribution.
105 Some examples are given below:
107 # You can access schema-level methods directly from the top-level model:
108 $c->model('FilmDB')->source(...);
109 $c->model('FilmDB')->resultset(...);
110 $c->model('FilmDB')->class(...);
111 $c->model('FilmDB')->any_other_schema_method(...);
113 # For resultsets, there's an even quicker shortcut:
114 $c->model('FilmDB::Actor')
115 # is the same as $c->model('FilmDB')->resultset('Actor')
119 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models. See
120 the documentation for L<Catalyst::Helper::Model::DBIC::Schema> for
121 information on generating these Models via Helper scripts.
123 =head1 CONFIG PARAMETERS
129 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
130 to be findable in C<@INC>, but it does not need to be inside the
131 C<Catalyst::Model::> namespace. This parameter is required.
135 This is an arrayref of connection parameters, which are specific to your
136 C<storage_type> (see your storage type documentation for more details).
138 This is not required if C<schema_class> already has connection information
139 defined inside itself (which isn't highly recommended, but can be done)
141 For L<DBIx::Class::Storage::DBI>, which is the only supported
142 C<storage_type> in L<DBIx::Class> at the time of this writing, the
143 parameters are your dsn, username, password, and connect options hashref.
145 See L<DBIx::Class::Storage::DBI/connect_info> for more details.
149 connect_info => [ 'dbi:Pg:dbname=mypgdb', 'postgres', '' ],
152 'dbi:SQLite:dbname=foo.db',
155 'PRAGMA synchronous = OFF',
161 'dbi:Pg:dbname=mypgdb',
167 'some SQL statement',
168 'another SQL statement',
175 Allows the use of a different C<storage_type> than what is set in your
176 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
187 Instantiates the Model based on the above-documented ->config parameters.
188 The only required parameter is C<schema_class>. C<connect_info> is
189 required in the case that C<schema_class> does not already have connection
190 information defined for it.
194 Tells the Catalyst component architecture that the encapsulated schema
195 object is to be returned for $c->model calls for this model name.
202 my $self = shift->NEXT::new(@_);
204 my $class = ref($self);
205 my $model_name = $class;
206 $model_name =~ s/^[\w:]+::(?:Model|M):://;
208 croak "->config->{schema_class} must be defined for this model"
209 unless $self->{schema_class};
211 my $schema_class = $self->{schema_class};
213 $schema_class->require
214 or croak "Cannot load schema_class '$schema_class': $@";
216 my $schema_obj = $schema_class->clone;
217 $schema_obj->storage_type($self->{storage_type}) if $self->{storage_type};
218 $schema_obj->connection(@{$self->{connect_info}}) if $self->{connect_info};
220 if(!$schema_obj->storage) {
221 croak "Either ->config->{connect_info} must be defined for $class"
222 . " or $schema_class must have connect info defined on it. "
223 . "Here's what we got:\n"
227 $self->{schema_obj} = $schema_obj;
230 foreach my $moniker ($self->schema->sources) {
231 my $classname = "${class}::$moniker";
232 # XXX -- Does this need to be dynamic, or can it be done w/ COMPONENT too?
233 *{"${classname}::ACCEPT_CONTEXT"} = sub {
235 shift->model($model_name)->resultset($moniker);
242 sub COMPONENT { shift->{schema_obj} }
246 General Catalyst Stuff:
248 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
249 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
251 Stuff related to DBIC and this Model style:
253 L<DBIx::Class>, L<DBIx::Class::Schema>,
254 L<DBIx::Class::Schema::Loader>, L<Catalyst::Helper::Model::DBIC::Schema>
258 Brandon L Black, C<blblack@gmail.com>
262 This program is free software, you can redistribute it and/or modify it
263 under the same terms as Perl itself.