1 package Catalyst::Model::DBIC::Schema;
4 use base qw/Catalyst::Base Class::Accessor::Fast Class::Data::Accessor/;
6 use UNIVERSAL::require;
11 __PACKAGE__->mk_classdata('composed_schema');
12 __PACKAGE__->mk_accessors('schema');
16 Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class
20 package MyApp::Model::Foo;
22 use base 'Catalyst::Model::DBIC::Schema';
25 schema_class => 'Foo::SchemaClass',
26 connect_info => [ 'dbi:Pg:dbname=foodb',
37 # ->schema To access schema methods:
38 $c->model('Foo')->schema->source(...);
40 # Shortcut to the schema resultset monikers for ->search et al:
41 $c->model('Foo::Bar')->search(...);
42 # is the same as $c->model('Foo')->schema->resultset('Bar')->search(...);
44 # To get the composed schema for making new connections:
45 my $newconn = $c->model('Foo')->composed_schema->connect(...);
47 # Or the same thing via a convenience shortcut:
48 my $newconn = $c->model('Foo')->connect(...);
50 # or, if your schema works on different storage drivers:
51 my $newconn = $c->model('Foo')->composed_schema->clone();
52 $newconn->storage_type('::LDAP');
53 $newconn->connect(...);
55 # and again, a convenience shortcut
56 my $newconn = $c->model('Foo')->clone();
57 $newconn->storage_type('::LDAP');
58 $newconn->connect(...);
62 This is a Catalyst Model for L<DBIx::Class::Schema>-based Models.
64 =head1 CONFIG PARAMETERS
70 This is the classname of your L<DBIx::Class::Schema> Schema. It needs
71 to be findable in C<@INC>, but it does not need to be underneath
76 This is an arrayref of connection parameters, which are specific to your
77 C<storage_type>. For C<::DBI>, which is the only supported C<storage_type>
78 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
79 dsn, username, password, and connect options hashref.
83 Allows the use of a different C<storage_type> than what is set in your
84 C<schema_class> (which in turn defaults to C<::DBI> if not set in current
85 L<DBIx::Class>). Completely optional, and probably unneccesary for most
86 people, until other storage backends become available for L<DBIx::Class>.
96 Instantiates the Model based on the above-documented ->config parameters.
100 Accessor which returns the connected schema being used by the this model.
102 =item composed_schema
104 Accessor which returns the composed schema, which has no connection info,
105 which was used in constructing the C<schema> above. Useful for creating
106 new connections based on the same schema/model.
110 Shortcut for ->composed_schema->clone
114 Shortcut for ->composed_schema->connect
121 my ( $self, $c ) = @_;
122 $self = $self->NEXT::new($c);
124 my $class = ref($self);
125 my $model_name = $class;
126 $model_name =~ s/^[\w:]+::(?:Model|M):://;
128 foreach (qw/ connect_info schema_class /) {
129 croak "->config->{$_} must be defined for this model"
133 my $schema_class = $self->{schema_class};
135 $schema_class->require
136 or croak "Cannot load schema class '$schema_class': $@";
138 $self->composed_schema($schema_class->compose_namespace($class));
139 $self->schema($self->composed_schema->clone);
140 $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
141 $self->schema->connect(@{$self->{connect_info}});
144 foreach my $moniker ($self->schema->sources) {
145 *{"${class}::${moniker}::ACCEPT_CONTEXT"} = sub {
147 shift->model($model_name)->schema->resultset($moniker);
155 sub clone { shift->composed_schema->clone(@_); }
158 sub connect { shift->composed_schema->connect(@_); }
162 L<Catalyst>, L<DBIx::Class>, L<DBIx::Class::Schema>,
163 L<DBIx::Class::Schema::Loader>
167 Brandon L Black, C<blblack@gmail.com>
171 This program is free software, you can redistribute it and/or modify it
172 under the same terms as Perl itself.