[Catalyst] Accessing $c from Model

Matt S Trout dbix-class at trout.me.uk
Thu Feb 1 09:35:48 GMT 2007


On 31 Jan 2007, at 19:27, Juan Miguel Paredes wrote:

> 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;
> }

package MyApp::DataStore; # or whatever this is called

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

__PACKAGE__->mk_group_accessors(simple => 'context');

then

my $new = bless ...

$new->schema(bless(...)); # same trick on the schema

$new->schema->context($c);

then in the DBIC code

$self->result_source->schema->context;

make more sense?

-- 
Matt S Trout, Technical Director, Shadowcat Systems Ltd.
Offering custom development, consultancy and support contracts for  
Catalyst,
DBIx::Class and BAST. Contact mst (at) shadowcatsystems.co.uk for  
details.
+ Help us build a better perl ORM: http://dbix- 
class.shadowcatsystems.co.uk/ +





More information about the Catalyst mailing list