1 package Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema;
4 use MooseX::MarkAsMethods autoclean => 1;
6 with 'Catalyst::Component::InstancePerContext';
10 Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema - Clone the schema
11 with attributes for each requests
16 traits => ['PerRequestSchema'],
19 sub per_request_schema_attributes {
21 return (restricting_object => $c->user->obj);
24 sub per_request_schema {
26 return $self->schema->schema_method($c->user->obj)
32 Clones the schema for each new request with the attributes retrieved from your
33 C<per_request_schema_attributes> method, which you must implement. This method
34 is passed the context.
36 Alternatively, you could also override the C<per_request_schema> method if you
37 need access to the schema clone and/or need to separate out the Model/Schema
38 methods. (See examples above and the defaults in the code.)
42 sub build_per_context_instance {
43 my ( $self, $ctx ) = @_;
44 return $self unless blessed($ctx);
46 my $new = bless {%$self}, ref $self;
48 $new->schema($new->per_request_schema($ctx));
53 # Thanks to Matt Trout for this idea
54 sub per_request_schema {
56 return $self->schema->clone($self->per_request_schema_attributes($c));
59 ### TODO: This should probably be more elegant ###
60 sub per_request_schema_attributes {
61 confess "Either per_request_schema_attributes needs to be created, or per_request_schema needs to be overridden!";
67 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class::Schema>
71 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
72 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
76 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
80 This program is free software, you can redistribute it and/or modify it
81 under the same terms as Perl itself.