[Catalyst] ACCEPT_CONTEXT in Catalyst::Model

Stephen Shorrock stephen.shorrock at gmail.com
Tue Mar 6 15:18:36 GMT 2012


Hi,

I was wondering whether anybody could offer some advice.

I'm trying to create some Moose objects within my Catalyst 5.9
application that have access to the models within the application.  To
do this I have attempted to reuse some code that is working on
Catalyst version 5.8.  Essentially to get access to the model I am
creating a base class with accessors subroutines and then using these
in all the children.  Trouble the $self->context value does not seem
to persist.

I've attached the code I was using.  Have I made an elementary
mistake?  I've reviewed what works in 5.8 many times any other than
the call to __PACKAGE__->mk_accessors(qw|context|) 9it fails with that
also) the code looks identical.

Thanks in advance

Stephen



Code somewhere in view  / controller:

my $user = $c->model("MyBaseObject::User")->new(id=>"someid");
print "user id: ".$user->id;
$user->pop_costcentres; #Fails here


Using Objects:

package CUFSE::Model::MyBaseObject;
use parent "Catalyst::Model";
use Moose;

#__PACKAGE__->mk_accessors(qw|context|);
has 'context'=>(is=>'rw', isa=>'Object');

sub ACCEPT_CONTEXT {
    my ($self, $c, @args) = @_;
    $self->context($c);
    print STDERR "set context\n" if (defined($self->context));
    return $self;
}

sub get_dbmodel {
    my ($self) = shift;
    print STDERR "context set\n" if (defined($self->context));
    return  $self->context->model('Finance'); FAILS HERE
$self->context is not set
}

no Moose;
__PACKAGE__->meta->make_immutable;


# ------------------------------

package CUFSE::Model::MyBaseObject::User;

use Moose;

BEGIN { extends 'CUFSE::Model::MyBaseObject' };

has 'id'=>(is=>'ro');

sub pop_costcentres {
    my $self = shift;
    print STDERR "pre dbmodel get\n";
    my @dels = $self->get_dbmodel->resultset('Delegates')->search();
#FAILS HERE
    map { push @{$self->costcentres}, $_->del_id; } @dels;
};

no Moose;
__PACKAGE__->meta->make_immutable;



More information about the Catalyst mailing list