[Catalyst] Models Inheriting from Catalyst::Base

Bill Moseley moseley at hank.org
Thu Nov 17 22:36:56 CET 2005


On Thu, Nov 17, 2005 at 02:56:30PM -0600, Brandon Black wrote:
> Inheriting your Model from Catalyst::Model::* does not give your Model
> access to $c, but it does give your controllers access to your Model
> via:
> 
> $c->comp('App::M::CDBI::MyTable')->a_table_method();

I was asking about inheriting from Catalyst::Base (or
Catalyst::Model):

    package App::M::CDBI;
    use strict;
    use base 'Catalyst::Model';
    use DB;
    DB->new( App->config->{database} );

    sub new {
        my ( $self, $c ) = @_;
        $self = $self->NEXT::new( $c );
        use DateTime;
        $c->components->{sometime} = DateTime->now;
    }
    1;



$c is the App base class, and $self is App::M::CDBI in that example.

Then in a controller you can do:

    my $dt = $c->comp('sometime');
    warn "dt = $dt\n";

And then you might see something like this:

    dt = 2005-11-17T21:33:15


What use that is, I'm not sure yet. ;)

I suppose you could do:

    $c->components->{'Foo::BAR::sometime'} = DateTime->now;

And still get to that component without knowing its full name.

    my $dt = $c->comp('sometime');  # still works



-- 
Bill Moseley
moseley at hank.org




More information about the Catalyst mailing list