Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Accessing $c from Model

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


mark at itsolve

Dec 27, 2006, 4:08 AM

Post #1 of 22 (1600 views)
Permalink
Accessing $c from Model

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

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


lorn.br at gmail

Dec 27, 2006, 4:55 AM

Post #2 of 22 (1567 views)
Permalink
Re: Accessing $c from Model [In reply to]

you can passa the $c from controller to model
$c->model(foo)->bar($c,$foobar);

On 12/27/06, Mark Zealey <mark[at]itsolve.co.uk> 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
>
> _______________________________________________
> List: Catalyst[at]lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>



--
Lorn
- Slackware Linux
www.slackwarezine.com.br
- http://lornlab.org


ash at cpan

Dec 27, 2006, 5:01 AM

Post #3 of 22 (1563 views)
Permalink
Re: Accessing $c from Model [In reply to]

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

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


jjn1056 at yahoo

Dec 27, 2006, 7:02 AM

Post #4 of 22 (1566 views)
Permalink
Re: Accessing $c from Model [In reply to]

--- Lorn <lorn.br[at]gmail.com> wrote:

> you can passa the $c from controller to model
> $c->model(foo)->bar($c,$foobar);

I'd vote for this method over setting the context,
since one of the goals of a model is to not care about
how or when it's called, that way you are encouraging
yourself to not build a fragile application where
everything is interdependent. Actually I'd take it a
step further and only pass the stuff from the context
that you need, like $c->request if you need the
request object, etc.

That being said I have used the method suggested by
another responder for grabbing the context, mostly so
that I can do debugging inside the model
($c->log->...). If there was better way to access the
log object from views and models that don't require
getting the context or getting $c->log than I am sure
I'd never need it.

A seemless way to share $c->log with DBIx::Class
objects would also be nice.

Also, as an aside, if you are building a custom logger
it might be more fun to create your own logger object
so that you can do $c->log->mylogger(...), or I
believe you can use the Log4Perl catalyst plugin and
configure it to log to a databse. That way you can
skip the whole creating a custom model for this need.

--john

>
> On 12/27/06, Mark Zealey <mark[at]itsolve.co.uk> 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
> >
> > _______________________________________________
> > List: Catalyst[at]lists.rawmode.org
> > Listinfo:
> http://lists.rawmode.org/mailman/listinfo/catalyst
> > Searchable archive:
> >
>
http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> > Dev site: http://dev.catalyst.perl.org/
> >
>
>
>
> --
> Lorn
> - Slackware Linux
> www.slackwarezine.com.br
> - http://lornlab.org
> > _______________________________________________
> List: Catalyst[at]lists.rawmode.org
> Listinfo:
> http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive:
>
http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


mark at itsolve

Dec 27, 2006, 8:15 AM

Post #5 of 22 (1554 views)
Permalink
Re: Accessing $c from Model [In reply to]

On Wednesday 27 December 2006 1:01 pm, Ash Berlin wrote:
> 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;
> }

Isn't that really really slow though? Constructing a new object for each call?

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


edencardim at gmail

Dec 27, 2006, 8:32 AM

Post #6 of 22 (1562 views)
Permalink
Re: Accessing $c from Model [In reply to]

On 12/27/06, Mark Zealey <mark[at]itsolve.co.uk> 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?

What's wrong with $c->forward('Model::Log', 'info', ['foo']) ?

--
Eden Cardim
Instituto Baiano de Biotecnologia
Núcleo de Biologia Computacional e Gestão de Informações Biotecnológicas
Laboratório de Bioinformática
--
"you seem to think that 'close enough' is close enough...
please learn to be 'literal' around programming."
merlyn - on irc.freenode.net#perl

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


jon at jrock

Dec 27, 2006, 12:14 PM

Post #7 of 22 (1567 views)
Permalink
Re: Accessing $c from Model [In reply to]

> Isn't that really really slow though? Constructing a new object for each call?

No, it's not. Creating an object in Perl amounts to setting a flag (the
OBJECT flag in subclasses of SvPVMG, to be exact).

See illguts: http://gisle.aas.no/perl/illguts/

--
package JAPH;use Catalyst qw/-Debug/;($;=JAPH)->config(name => do {
$,.=reverse qw[Jonathan tsu rehton lre rekca Rockway][$_].[split //,
";$;"]->[$_].q; ;for 1..4;$,=~s;^.;;;$,});$;->setup;

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


jjn1056 at yahoo

Dec 27, 2006, 12:54 PM

Post #8 of 22 (1565 views)
Permalink
Re: Accessing $c from Model [In reply to]

--- Eden Cardim <edencardim[at]gmail.com> wrote:

> On 12/27/06, Mark Zealey <mark[at]itsolve.co.uk> 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?
>
> What's wrong with $c->forward('Model::Log', 'info',
> ['foo']) ?

This discussion is bring back old nightmares for me :)
Catalyst is really flexible but I personally haven't
really figured out the value of:

## Forward style
$c->forward('Model::Log', 'info', ['foo'])

versus

## direct invocation style
$c->model('Log')->info("foo") with/without using
ACCEPT_CONTEXT to have this instantiate a new object
for each request

versus

## Classic Perl style
Use Model::Log;

my $log = Model::Log->new(...);
$log->info($c,'foo');

I tend to use forwarding mostly for controllers,
although with Chaining I find I need this less and
less. I also use forward to delegate to a view
handler.

>From what I can see the main advantage of forward is
that it sends the context for you automatically. Also
you get the automatic return but this to me seems less
important with a model and more relevant to
controllers. Usually my models return self or some
sort of value anyway.

The only advantage I can see for making your business
objects into catalyst models is that you get the handy
config methods that you can centralize in your main
configuration file if you like. And it saves you
having to add 'use ...' at the top of all the
controllers that need a particular model (although you
can create a base controller to help with this issue).

I'm certainly offtopic by now but this question gets
asked of me by my coworkers and I wish I had more than
a list of pro/cons to give as an answer.

--john

>
> --
> Eden Cardim
> Instituto Baiano de Biotecnologia
> Núcleo de Biologia Computacional e Gestão de
> Informações Biotecnológicas
> Laboratório de Bioinformática
> --
> "you seem to think that 'close enough' is close
> enough...
> please learn to be 'literal' around programming."
> merlyn - on irc.freenode.net#perl
>
> _______________________________________________
> List: Catalyst[at]lists.rawmode.org
> Listinfo:
> http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive:
>
http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


pagaltzis at gmx

Dec 27, 2006, 6:31 PM

Post #9 of 22 (1562 views)
Permalink
Re: Accessing $c from Model [In reply to]

* Jonathan Rockway <jon[at]jrock.us> [2006-12-27 21:25]:
> No, it's not. Creating an object in Perl amounts to setting a
> flag (the OBJECT flag in subclasses of SvPVMG, to be exact).
>
> See illguts: http://gisle.aas.no/perl/illguts/

Are you being too literal on purpose? Yeah blessing a ref is just
setting a flag but you need a referee for that ref and people
don’t consider its creation a separate step in general.


* Mark Zealey <mark[at]itsolve.co.uk> [2006-12-27 17:20]:
> On Wednesday 27 December 2006 1:01 pm, Ash Berlin wrote:
> > 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;
> > }
>
> Isn't that really really slow though? Constructing a new object
> for each call?

Well, it depends. If you call `$c->Model('Log')` a ton of times,
then it will indeed be slow. If you don’t, you won’t notice.

If you’ve *determined* from profiling that `$c->Model('Log')` is
called enough to be a bottleneck, you have two options. The
trivial one is that to store the object returned in a variable
and then make your logging calls on that variable, so you don’t
go through `ACCEPT_CONTEXT` constantly. The other is to memoise
`ACCEPT_CONTEXT` so when it’s passed the same `$c`, it always
returns the same copy. You’ll have to be very careful about your
cache though; it’s easy to introduce leaks. If Catalyst depends
critically on object destruction timing, you might even break the
whole thing alltogether.

In summary: avoid caring too much without specific reason to.

Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


danielmcbrearty at gmail

Dec 29, 2006, 11:47 AM

Post #10 of 22 (1547 views)
Permalink
Re: Re: Accessing $c from Model [In reply to]

FWIW ... : what I've noticed about using models (or not) ... :

1. the advantage of using a model mostly seems to be that it
autoloads, and then is accessible everywhere from $c. Otherwise, there
doesn't seem to be much difference from just having a normal perl
library.

2. so if you just need data/logic for use in one controller, a
standard library may be better.

3. usually, I'm finding its better to try to keep the models as pure
data sources, and not have them interact, or be context dependent. So
I try to make it the controllers job to do anything that involves
context, or to connect different models together where necessary. That
seems to result in a cleaner API. I have started writing modules that
took $c as an argument, but generally took it out later.

4. of course there are exceptions ... which is why a framework that
has flexibility and power is worth investing time to learn, even if it
takes longer than one that says "we do it this way around here ..."

this is just ideas that I've seen starting to emerge from many
learner's mistakes ... others will know different / better ...


On 12/28/06, A. Pagaltzis <pagaltzis[at]gmx.de> wrote:
> * Jonathan Rockway <jon[at]jrock.us> [2006-12-27 21:25]:
> > No, it's not. Creating an object in Perl amounts to setting a
> > flag (the OBJECT flag in subclasses of SvPVMG, to be exact).
> >
> > See illguts: http://gisle.aas.no/perl/illguts/
>
> Are you being too literal on purpose? Yeah blessing a ref is just
> setting a flag but you need a referee for that ref and people
> don't consider its creation a separate step in general.
>
>
> * Mark Zealey <mark[at]itsolve.co.uk> [2006-12-27 17:20]:
> > On Wednesday 27 December 2006 1:01 pm, Ash Berlin wrote:
> > > 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;
> > > }
> >
> > Isn't that really really slow though? Constructing a new object
> > for each call?
>
> Well, it depends. If you call `$c->Model('Log')` a ton of times,
> then it will indeed be slow. If you don't, you won't notice.
>
> If you've *determined* from profiling that `$c->Model('Log')` is
> called enough to be a bottleneck, you have two options. The
> trivial one is that to store the object returned in a variable
> and then make your logging calls on that variable, so you don't
> go through `ACCEPT_CONTEXT` constantly. The other is to memoise
> `ACCEPT_CONTEXT` so when it's passed the same `$c`, it always
> returns the same copy. You'll have to be very careful about your
> cache though; it's easy to introduce leaks. If Catalyst depends
> critically on object destruction timing, you might even break the
> whole thing alltogether.
>
> In summary: avoid caring too much without specific reason to.
>
> Regards,
> --
> Aristotle Pagaltzis // <http://plasmasturm.org/>
>
> _______________________________________________
> List: Catalyst[at]lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>


--
Daniel McBrearty
email : danielmcbrearty at gmail.com
www.engoi.com : the multi - language vocab trainer
BTW : 0873928131

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


claco at chrislaco

Dec 29, 2006, 11:56 AM

Post #11 of 22 (1550 views)
Permalink
Re: Re: Accessing $c from Model [In reply to]

Daniel McBrearty wrote:
> FWIW ... : what I've noticed about using models (or not) ... :
>
> 1. the advantage of using a model mostly seems to be that it
> autoloads, and then is accessible everywhere from $c. Otherwise, there
> doesn't seem to be much difference from just having a normal perl
> library.
>
> 2. so if you just need data/logic for use in one controller, a
> standard library may be better.

I generally agree, with one more note. A model that can have different
settings for itself inside of Catalyst than it does as a standard module
outside of Catalyst.

Even then [for me], my Models are usual just glue between Cat specific
settings and the non-Cat perl library.

-=Chris
Attachments: signature.asc (0.18 KB)


juan.paredes at gmail

Jan 31, 2007, 7:23 AM

Post #12 of 22 (1507 views)
Permalink
Re: Accessing $c from Model [In reply to]

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

Thanks in advance,

Juan.

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


dbix-class at trout

Jan 31, 2007, 7:51 AM

Post #13 of 22 (1510 views)
Permalink
Re: Accessing $c from Model [In reply to]

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



_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


juan.paredes at gmail

Jan 31, 2007, 11:27 AM

Post #14 of 22 (1508 views)
Permalink
Re: Accessing $c from Model [In reply to]

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

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


dbix-class at trout

Feb 1, 2007, 1:35 AM

Post #15 of 22 (1501 views)
Permalink
Re: Accessing $c from Model [In reply to]

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



_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


juan.paredes at gmail

Feb 1, 2007, 7:30 AM

Post #16 of 22 (1503 views)
Permalink
Re: Accessing $c from Model [In reply to]

On 2/1/07, Matt S Trout <dbix-class[at]trout.me.uk> wrote:
>
> 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?
>

Indeed, your suggestion worked fine, thanks a lot! Just had to add
DBIx::Class::AccessorGroup to the schema class

Regards,

Juan.

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


smoothhound at gmail

Feb 27, 2007, 3:25 AM

Post #17 of 22 (1391 views)
Permalink
Re: Accessing $c from Model [In reply to]

Hi,

This is exactly what I want to do, however I can't seem to get my head round
how to implement this solution, I've tried various incarnations but I don't
seem to be getting anywhere.

Annotations below, thanks for patience.

Scott


On 2/1/07, Juan Miguel Paredes <juan.paredes[at]gmail.com> wrote:
>
> On 2/1/07, Matt S Trout <dbix-class[at]trout.me.uk> wrote:
> >
> > package MyApp::DataStore; # or whatever this is called
> >
> > use base qw/DBIx::Class::Schema/;
> >
> > __PACKAGE__->mk_group_accessors(simple => 'context');



I get this bit, (also add DBIx::Class::AccessorGroup to the base class list)


> then
> >
> > my $new = bless ...
> >



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


Is the above in the same file? what should replace the ellipsis

> then in the DBIC code
> >
> > $self->result_source->schema->context;
> >
> > make more sense?
> >


Where exactly is the DBIC code? MyApp::Model::DataStore?

Indeed, your suggestion worked fine, thanks a lot! Just had to add
> DBIx::Class::AccessorGroup to the schema class
>
> Regards,
>
> Juan.
>
> _______________________________________________
> List: Catalyst[at]lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>


smoothhound at gmail

Mar 5, 2007, 4:02 AM

Post #18 of 22 (1356 views)
Permalink
Re: Accessing $c from Model [In reply to]

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.


On 2/27/07, Scott Thomson <smoothhound[at]gmail.com> wrote:
> Hi,
>
> This is exactly what I want to do, however I can't seem to get my head round
> how to implement this solution, I've tried various incarnations but I don't
> seem to be getting anywhere.
>
> Annotations below, thanks for patience.
>
> Scott
>
>
> On 2/1/07, Juan Miguel Paredes <juan.paredes[at]gmail.com> wrote:
> > On 2/1/07, Matt S Trout <dbix-class[at]trout.me.uk> wrote:
> > >
> > > package MyApp::DataStore; # or whatever this is called
> > >
> > > use base qw/DBIx::Class::Schema/;
> > >
> > > __PACKAGE__->mk_group_accessors(simple => 'context');
>
>
>
> I get this bit, (also add DBIx::Class::AccessorGroup to the base class list)
>
> > > then
> > >
> > > my $new = bless ...
> > >
>
> > > $new->schema(bless(...)); # same trick on the schema
> > >
> > > $new->schema->context($c);
>
> Is the above in the same file? what should replace the ellipsis
>
> > > then in the DBIC code
> > >
> > > $self->result_source->schema->context;
> > >
> > > make more sense?
> > >
>
>
> Where exactly is the DBIC code? MyApp::Model::DataStore?
>
>
> > Indeed, your suggestion worked fine, thanks a lot! Just had to add
> > DBIx::Class::AccessorGroup to the schema class
> >
> > Regards,
> >
> > Juan.
> >
> > _______________________________________________
> > List: Catalyst[at]lists.rawmode.org
> > Listinfo:
> http://lists.rawmode.org/mailman/listinfo/catalyst
> > Searchable archive:
> http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> > Dev site: http://dev.catalyst.perl.org/
> >
>
>

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


juan.paredes at gmail

Mar 5, 2007, 1:52 PM

Post #19 of 22 (1359 views)
Permalink
Re: Accessing $c from Model [In reply to]

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.

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


smoothhound at gmail

Mar 6, 2007, 12:43 AM

Post #20 of 22 (1355 views)
Permalink
Re: Accessing $c from Model [In reply to]

Excellent, that works - Thank you!

On 3/5/07, Juan Miguel Paredes <juan.paredes[at]gmail.com> wrote:
> 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.
>
> _______________________________________________
> List: Catalyst[at]lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>

_______________________________________________
List: Catalyst[at]lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


email at jasonkohles

Mar 6, 2007, 11:51 AM

Post #21 of 22 (1353 views)
Permalink
Re: Accessing $c from Model [In reply to]

On Mar 5, 2007, at 7:02 AM, Scott Thomson 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;
>
Very Bad Things are likely to happen if you ever attempt to run your
application under the debugger when you have a package named 'DB'.
I'd recommend using a different name...

--
Jason Kohles
email[at]jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing." -- Voltaire


smoothhound at gmail

Mar 6, 2007, 12:28 PM

Post #22 of 22 (1351 views)
Permalink
Re: Accessing $c from Model [In reply to]

Noted and actioned, Cheers.

Very Bad Things are likely to happen if you ever attempt to run your
> application under the debugger when you have a package named 'DB'. I'd
> recommend using a different name...
>
> --
> Jason Kohles
> email[at]jasonkohles.com
> http://www.jasonkohles.com/
> "A witty saying proves nothing." -- Voltaire
>
>
>
> _______________________________________________
> List: Catalyst[at]lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst[at]lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>
>

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.