[Catalyst] Accessing $c from Model

Matt S Trout dbix-class at trout.me.uk
Wed Jan 31 15:51:59 GMT 2007


On 31 Jan 2007, at 15:23, Juan Miguel Paredes wrote:

> On 12/27/06, Ash Berlin <ash at cpan.org> wrote:
>> Mark Zealey wrote:
>> > Hi there,
>> >
>> > I'm basically wanting to write a simple log function which logs  
>> hits on my
>> > website as entries in a database (automatically adding $c->user-> 
>> {id} and
>> > $c->req->referrer etc), but to do so I want to use a model (I  
>> think). Any
>> > ideas how I can just say $c->model('Log')->info("foo") and  
>> automatically get
>> > $c passed in? I think I could have sth like:
>> >
>> > package MyApp::Model::Log;
>> > use base 'Catalyst::Model';
>> >
>> > my $last_c;
>> >
>> > sub ACCEPT_CONTEXT {
>> >       my ($self, $c) = @_;
>> >       $last_c = $c;
>> > }
>> >
>> > sub info {
>> >       my ($self, $msg) = @_
>> >       my $c = $last_c;
>> >       ...
>> > }
>> >
>> > but this seems pretty messy...
>> >
>> > Mark
>> >
>>
>> Very very *VERY* bad idea.
>>
>> __PACKAGE__->mk_accessors(context);
>>
>> sub ACCEPT_CONTEXT {
>>    my ($self, $c, @args) = @_;
>>
>>    my $new = bless({ %$self }, ref $self);
>>    $new->context($c);
>>    return $new;
>> }
>>
>>
>> Something like the above instead.
>>
>> Ash
>
> 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

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