2 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
5 Manual creation of a DBIx::Class::Schema and a
6 Catalyst::Model::DBIC::Schema:
8 1. Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm:
10 package MyApp::Schema::FilmDB;
11 use base qw/DBIx::Class::Schema/;
13 __PACKAGE__->load_classes(qw/Actor Role/);
15 2. Create some classes for the tables in the database, for example an
16 Actor in MyApp/Schema/FilmDB/Actor.pm:
18 package MyApp::Schema::FilmDB::Actor;
19 use base qw/DBIx::Class/
21 __PACKAGE__->load_components(qw/Core/);
22 __PACKAGE__->table('actor');
26 and a Role in MyApp/Schema/Role.pm:
28 package MyApp::Schema::FilmDB::Role;
29 use base qw/DBIx::Class/
31 __PACKAGE__->load_components(qw/Core/);
32 __PACKAGE__->table('role');
36 Notice that the schema is in MyApp::Schema, not in MyApp::Model.
37 This way it's usable as a standalone module and you can test/run it
40 3. To expose it to Catalyst as a model, you should create a DBIC Model
41 in MyApp/Model/FilmDB.pm:
43 package MyApp::Model::FilmDB;
44 use base qw/Catalyst::Model::DBIC::Schema/;
47 schema_class => 'MyApp::Schema::FilmDB',
56 See below for a full list of the possible config parameters.
58 Now you have a working Model, accessing your separate DBIC Schema. Which
59 can be used/accessed in the normal Catalyst manner, via $c->model():
61 my $actor = $c->model('FilmDB::Actor')->find(1);
63 You can also use it to set up DBIC authentication with
64 Authentication::Store::DBIC in MyApp.pm:
68 use Catalyst qw/... Authentication::Store::DBIC/;
72 __PACKAGE__->config->{authentication}{dbic} = {
73 user_class => 'FilmDB::Actor',
75 password_field => 'password'
78 "$c->model()" returns a DBIx::Class::ResultSet for the source name
79 parameter passed. To find out more about which methods can be called on
80 a ResultSet, or how to add your own methods to it, please see the
81 ResultSet documentation in the DBIx::Class distribution.
83 Some examples are given below:
85 # to access schema methods directly:
86 $c->model('FilmDB')->schema->source(...);
88 # to access the source object, resultset, and class:
89 $c->model('FilmDB')->source(...);
90 $c->model('FilmDB')->resultset(...);
91 $c->model('FilmDB')->class(...);
93 # For resultsets, there's an even quicker shortcut:
94 $c->model('FilmDB::Actor')
95 # is the same as $c->model('FilmDB')->resultset('Actor')
97 # To get the composed schema for making new connections:
98 my $newconn = $c->model('FilmDB')->composed_schema->connect(...);
100 # Or the same thing via a convenience shortcut:
101 my $newconn = $c->model('FilmDB')->connect(...);
103 # or, if your schema works on different storage drivers:
104 my $newconn = $c->model('FilmDB')->composed_schema->clone();
105 $newconn->storage_type('::LDAP');
106 $newconn->connection(...);
108 # and again, a convenience shortcut
109 my $newconn = $c->model('FilmDB')->clone();
110 $newconn->storage_type('::LDAP');
111 $newconn->connection(...);
114 This is a Catalyst Model for DBIx::Class::Schema-based Models. See the
115 documentation for Catalyst::Helper::Model::DBIC::Schema for information
116 on generating these Models via Helper scripts.
120 This is the classname of your DBIx::Class::Schema Schema. It needs
121 to be findable in @INC, but it does not need to be inside the
122 "Catalyst::Model::" namespace. This parameter is required.
125 This is an arrayref of connection parameters, which are specific to
126 your "storage_type" (see your storage type documentation for more
129 This is not required if "schema_class" already has connection
130 information defined inside itself (which isn't highly recommended,
133 For DBIx::Class::Storage::DBI, which is the only supported
134 "storage_type" in DBIx::Class at the time of this writing, the
135 parameters are your dsn, username, password, and connect options
138 See "connect_info" in DBIx::Class::Storage::DBI for a detailed
139 explanation of the arguments supported.
143 connect_info => [ 'dbi:Pg:dbname=mypgdb', 'postgres', '' ],
146 'dbi:SQLite:dbname=foo.db',
149 'PRAGMA synchronous = OFF',
155 'dbi:Pg:dbname=mypgdb',
161 'some SQL statement',
162 'another SQL statement',
168 Allows the use of a different "storage_type" than what is set in
169 your "schema_class" (which in turn defaults to "::DBI" if not set in
170 current DBIx::Class). Completely optional, and probably unnecessary
171 for most people until other storage backends become available for
175 new Instantiates the Model based on the above-documented ->config
176 parameters. The only required parameter is "schema_class".
177 "connect_info" is required in the case that "schema_class" does not
178 already have connection information defined for it.
181 Accessor which returns the connected schema being used by the this
182 model. There are direct shortcuts on the model class itself for
183 schema->resultset, schema->source, and schema->class.
186 Accessor which returns the composed schema, which has no connection
187 info, which was used in constructing the "schema" above. Useful for
188 creating new connections based on the same schema/model. There are
189 direct shortcuts from the model object for composed_schema->clone
190 and composed_schema->connect
193 Shortcut for ->composed_schema->clone
196 Shortcut for ->composed_schema->connect
199 Shortcut for ->schema->source
202 Shortcut for ->schema->class
205 Shortcut for ->schema->resultset
208 Provides an accessor for the connected schema's storage object. Used
209 often for debugging and controlling transactions.
212 General Catalyst Stuff:
214 Catalyst::Manual, Catalyst::Test, Catalyst::Request, Catalyst::Response,
215 Catalyst::Helper, Catalyst,
217 Stuff related to DBIC and this Model style:
219 DBIx::Class, DBIx::Class::Schema, DBIx::Class::Schema::Loader,
220 Catalyst::Helper::Model::DBIC::Schema
223 Brandon L Black, "blblack@gmail.com"
226 This program is free software, you can redistribute it and/or modify it
227 under the same terms as Perl itself.