[Catalyst] Accessing $c from Model

Juan Miguel Paredes juan.paredes at gmail.com
Wed Jan 31 19:27:03 GMT 2007


On 1/31/07, Matt S Trout <dbix-class at trout.me.uk> wrote:
>
> On 31 Jan 2007, at 15:23, Juan Miguel Paredes wrote:
>
> > Hi, all!
> >
> > What if we have to overload ACCEPT_CONTEXT to do something like this,
> > but using DBIC::Schema?  What would be the recommended inheritance
> > chain? Catalyst::Model::DBIC::Schema acts as a glue between the actual
> > Catalyst model and the Schema classes, so I'm a little confused about
> > where to start...  In this particular case, we'd like to access $c
> > from model in order to overload subroutines (trigger-like) to track,
> > for example, which user modified what...
>
> add an accessor to the schema, and do $schema->clone then hand that
> $c->user
>

Ok! I understand that ACCEPT_CONTEXT would have to be placed in a
package along the Catalyst::Base inheritance chain, so, a feasible
place would be in MyApp::Model::Schema (based on
Catalyst::Model::DBIC::Schema).  I've tried this:


package MyApp::Model::BD;

use strict;
use base 'Catalyst::Model::DBIC::Schema';

__PACKAGE__->mk_accessors( 'context' );

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

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

  return $new;
}

In fact, placing some $c->log->debug in ACCEPT_CONTEXT shows it runs
ok. The question is, if this is the right place to overload
ACCEPT_CONTEXT, how would I access the context from, say:

package MyApp::Schema::BD::TpRol;

use strict;
use base qw/DBIx::Class/;

#table, add_columns, etc.

sub update {
  my $self = shift;

  my $c = $self->context; # wrong, no method "context", because $self
is a DBIx::Class::Row

  my $c = MyApp::Model::BD->context; # wrong, complains that Can't use
string ("MyApp::Model::BD") as a HASH ref while "strict refs"

  return $self->next::method( @_ );
}


So, if the location for overloading ACCEPT_CONTEXT seems reasonable,
accessing it from overloaded subroutines in ResultSource would be the
last part of the puzzle.  In this case, just as a proof of concept,
I'm trying to access the whole $c, but later that could be refined
with accessors for, say, just $c->user, $c->request->params, etc.

Thanks again!

Juan



More information about the Catalyst mailing list