1 package Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy;
3 use namespace::autoclean;
5 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
6 use Catalyst::Model::DBIC::Schema::Types 'Schema';
10 Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy - Proxy Schema Methods and
15 Allows you to call your L<DBIx::Class::Schema> methods directly on the Model
16 instance, and passes config options to your L<DBIx::Class::Schema> and
17 L<DBIx::Class::ResultSet> attributes at C<BUILD> time.
19 Methods and attributes local to your C<Model> take precedence over
20 L<DBIx::Class::Schema> or L<DBIx::Class::ResultSet> methods and attributes.
22 =head1 CREATING SCHEMA CONFIG ATTRIBUTES
24 To create attributes in your C<Schema.pm>, use either Moose or
25 L<Class::Accessor::Grouped>, which is inherited from by all L<DBIx::Class>
26 classes automatically. E.g.:
28 __PACKAGE__->mk_group_accessors(simple => qw/
37 has config_key1 => (is => 'rw', default => 'default_value');
39 This code can be added after the md5sum on L<DBIx::Class::Schema::Loader>
42 At app startup, any non-local options will be passed to these accessors, and can
43 be accessed as usual via C<< $schema->config_key1 >>.
45 These config values go into your C<Model::DB> block, along with normal config
48 =head1 CREATING RESULTSET CONFIG ATTRIBUTES
50 You can create classdata on L<DBIx::Class::ResultSet> classes to hold values
51 from L<Catalyst> config.
53 The code for this looks something like this:
55 package MySchema::ResultSet::Foo;
57 use base 'DBIx::Class::ResultSet';
59 __PACKAGE__->mk_group_accessors(inherited => qw/
64 __PACKAGE__->rs_config_key1('default_value');
66 Or, if you prefer L<Moose>:
68 package MySchema::ResultSet::Foo;
72 use MooseX::ClassAttribute;
73 extends 'DBIx::Class::ResultSet';
75 sub BUILDARGS { $_[2] } # important
77 class_has rs_config_key1 => (is => 'rw', default => 'default_value');
81 __PACKAGE__->meta->make_immutable;
85 In your catalyst config, use the generated Model name as the config key, e.g.:
94 my ($self, $args) = @_;
96 my $schema = $self->schema;
98 my $was_mutable = $self->meta->is_mutable;
100 $self->meta->make_mutable;
101 $self->meta->add_attribute('schema',
104 handles => $self->_delegates # this removes the attribute too
106 $self->meta->make_immutable unless $was_mutable;
108 $self->schema($schema) if $schema;
112 my ($self, $args) = @_;
114 $self->_pass_options_to_schema($args);
116 for my $source ($self->schema->sources) {
117 my $config_key = 'Model::' . $self->model_name . '::' . $source;
118 my $config = $self->app_class->config->{$config_key};
120 $self->_pass_options_to_resultset($source, $config);
127 my $schema_meta = Class::MOP::Class->initialize($self->schema_class);
128 my @schema_methods = $schema_meta->get_all_method_names;
130 # combine with any already added by other schemas
132 @{ $self->meta->find_attribute_by_name('schema')->handles }
135 # now kill the attribute, otherwise add_attribute in BUILD will not do the right
136 # thing (it clears the handles for some reason.) May be a Moose bug.
137 eval { $self->meta->remove_attribute('schema') };
140 @schema_methods{ @schema_methods, @handles } = ();
141 @schema_methods = keys %schema_methods;
143 my @my_methods = $self->meta->get_all_method_names;
145 @my_methods{@my_methods} = ();
148 for my $method (@schema_methods) {
149 push @delegates, $method unless exists $my_methods{$method};
155 sub _pass_options_to_schema {
156 my ($self, $args) = @_;
158 my @attributes = map {
160 } $self->meta->get_all_attributes;
163 @attributes{@attributes} = ();
165 for my $opt (keys %$args) {
166 if (not exists $attributes{$opt}) {
167 next unless $self->schema->can($opt);
168 $self->schema->$opt($args->{$opt});
173 sub _pass_options_to_resultset {
174 my ($self, $source, $args) = @_;
176 for my $opt (keys %$args) {
177 my $rs_class = $self->schema->source($source)->resultset_class;
178 next unless $rs_class->can($opt);
179 $rs_class->$opt($args->{$opt});
185 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class::Schema>
189 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
190 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
194 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
198 This program is free software, you can redistribute it and/or modify it
199 under the same terms as Perl itself.