[Catalyst] Doing work inside the DBIx::Class model

Cian Barclay cianbarclay at gmail.com
Wed Feb 20 03:10:12 GMT 2008


Hello Catalysters,

I'm using Catalyst with a DBIx::Class model generated by
DBIx::Class::Schema::Loader.

I want to make my model do more work, rather than having my
controllers fiddling around with model objects doing things to them.
But I'm stuck on the bit where I want to get a resultset inside the
model's subroutines. In a controller I use $c->model("Foo::Bar") to
get the resultset. How can I get a resultset when I'm in the model?

I could pass $c in to the model, but then the model wouldn't be
usable without Catalyst. I'd like to be able to use the model
for other things, but from reading the DBIx::Class manual, it
seems like I'd need the $schema object to get resultsets.

Since I'm stuck, I thought it would be a good time to ask for some
help on this please. Is there a way to make a DBIx::Class model
which is usable both for Catalyst and something else? How do you
get resultsets inside the model which could either be from the
Catalyst model, or from the database connection if Catalyst wasn't
being used?

Any help would be appreciated.

For example:

package Foo::Controller::Bars;

sub do_something : Local
{
    my ($self, $c) =3D @_;
    my $bar =3D $c->model("Foo::Bar")->find(77);
    $bar->set_widgets;
}

#-----

package Foo::Bar;

sub set_widgets
{
    my $self =3D shift;

    # here I want to use $c->model("Foo::Widget") so that I can
    # create or select widgets. But I have no $c.
    #
    # If the model was used by something non-Catalyst, presumably
    # there would need to be some way of getting resultsets here
    # from the other application too.

    my $widget_rs =3D $something->model("Foo::Widget");
    my $widget =3D $widget_rs->find_or_create({name =3D> 'foobar'});
    $self->widget($widget);
    $self->update;
}

#-----
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20080220/c659e=
a4c/attachment.htm


More information about the Catalyst mailing list