[Catalyst] Accessing $c from Model

Juan Miguel Paredes juan.paredes at gmail.com
Mon Mar 5 21:52:10 GMT 2007


On 3/5/07, Scott Thomson <smoothhound at gmail.com> wrote:
> Ok, I think I'm close...
>
> My main schema class:
>
> package DB;
>
> use base qw/DBIx::Class::Schema  DBIx::Class::AccessorGroup/;
>
> __PACKAGE__->mk_group_accessors(simple => 'context');
> __PACKAGE__->load_classes(qw//);
> 1;
>
>
> My model class...
>
> package WCN::Model::DB;
>
> use strict;
> use base qw/Catalyst::Model::DBIC::Schema::WCN/;
>
> __PACKAGE__->config(
>     schema_class => 'DB',
> );
>
> sub ACCEPT_CONTEXT {
>   my ($self, $c, @args) = @_;
>
>   $c->log->debug("  *** class: " . ref($self));
>
>   my $new = bless({%$self}, ref $self);
>   $new->schema(bless({%{$self->schema}}, ref($self->schema)));
>   $new->schema->context($c);
>
>   # Outputs 'WCN' which is correct
>   $c->log->debug("  *** ref of context in self: ",
> ref($new->schema->context()));
>
>   return $new;
> }
>
> The debug prints the correct class of $c when ACCEPT_CONTEXT is
> called, however $self->result_source->schema->context is not present
> in the model objects.
>
> Have I got the wrong end of the stick somewhere?
>
> Many Thanks,
>
> Scott.

That looks very similar to what we did, but in the model class:

sub ACCEPT_CONTEXT {
  my ($self, $c, @args) = @_;

  my $new = bless({ %$self }, ref $self);
  $new->schema->_context($c);
  return $new;
}

(we used _context for accesor)

Also, I don't know if the DB namespace is reserved for debug, and
could lead to complications... I think I read that in a nearby thread
a while ago, but can't locate it right now...

Regards,
Juan.



More information about the Catalyst mailing list