Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

I need help adding a new model to a legacy Catalyst setup

by LittleJack (Beadle)
on Mar 18, 2022 at 00:54 UTC ( [id://11142203]=perlquestion: print w/replies, xml ) Need Help??

LittleJack has asked for the wisdom of the Perl Monks concerning the following question:

I'm probably not doing this the right way, I should be generating the new model automatically through some kind of script?

But it's not a 100% standard Catalyst setup, and I'm kind of in a hurry.

I have a set of Models already in the site which look like this:

use utf8; package Model::DB::Result::Thing; use base 'DBIx::Class::Core'; use strict; use warnings; __PACKAGE__->table("things"); # etc. etc. some fields are defined here __PACKAGE__->add_columns( "id", { data_type => "integer", is_auto_increment => 1, is_nullable => 0, }, "name", { data_type => "text", is_nullable => 1 },

So I need to add another Model along the lines of:

use utf8; package Model::DB::Result::OtherThing; use base 'DBIx::Class::Core'; use strict; use warnings; __PACKAGE__->table("otherthings"); # etc. etc. some fields are defined here __PACKAGE__->add_columns( "id", { data_type => "integer", is_auto_increment => 1, is_nullable => 0, }, "othername", { data_type => "text", is_nullable => 1 },

And I'm doing it by the naïve method of putting a new OtherThing.pm into the same directory, restarting, and trying to call on it from a Controller.

This doesn't work and I get [error] Caught exception in Console::Controller::OtherThing->create "Can't call method "create" on an undefined value at /var/www/Payment/Console/lib/Console/Controller/OtherThing.pm line xx."

Where the undefined thing is $c->model('OtherThing')

So is there some other flag or listing or module which controls which of my Models get loaded? There's a load_namespaces step in booting up Catalyst, I know that much.

Happy to do it the correct way via the correct script if I can, but I need to get it working either way.

I can see the Model being loaded when Catalyst starts up, by the way:

[debug] Loaded components: .--------------------------------------------------------------------- +------------------------------------+----------. | Class + | Type | +--------------------------------------------------------------------- +------------------------------------+----------+ | Console::Controller::OtherThing + | instance | | [snip] + | | | Console::Model::DB::Thing + | class | | Console::Model::DB::OtherThing + | class |

Replies are listed 'Best First'.
Re: I need help adding a new model to a legacy Catalyst setup
by Your Mother (Archbishop) on Mar 18, 2022 at 01:36 UTC

    What you’re doing is the right idea and should work if all the names and columns and such are right. Possibly the problem is the plural “s” or the CamelCasing.

    package Model::DB::Result::OtherThing; # <- No "s," and not Otherthing +. use base 'DBIx::Class::Core'; use strict; use warnings; __PACKAGE__->table("otherthings"); # <- "s"
Re: I need help adding a new model to a legacy Catalyst setup
by NERDVANA (Deacon) on Mar 20, 2022 at 04:14 UTC
    The model name is $c->model('DB::OtherThing'), you just forgot the prefix. The other way to access it is with
    my $db= $c->model('DB'); my $rs= $db->resultset('OtherThing');
    This gives you the bare ResultSet object without wrapping it with a Catalyst model object, which sometimes makes error messages clearer.

    For frequently used DBIC resultsets, it can be nice to add a convenience method to the application object (in Console.pm) like this:

    sub otherthing { my $self= shift; my $rs= $self->model('DB')->resultset('OtherThing'); return @_? $rs->find(shift) : $rs }
    Then you can call
    $c->otherthing($id); # fetch an OtherThing row by primary key $c->otherthing->search_rs(...)->all # convenient access to resultset

    There is no need to use the create scripts. I never do because it takes longer to remember the syntax for the create script and add my module boilerplate to the top of the file than to just "Save As" from the other file most like the one I want to create.

      The model name is $c->model('DB::OtherThing'), you just forgot the prefix.

      So simple and yet it had my stumped for hours. Thank you so much.

Re: I need help adding a new model to a legacy Catalyst setup
by Bod (Parson) on Mar 18, 2022 at 01:11 UTC
    ...and I'm kind of in a hurry

    Experience tells me that's kind of the wrong place to be...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11142203]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-26 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found