tempus71 tempus71 - 18 days ago 6
Perl Question

Something is wrong using Mojolicious::Plugin::Model

I'm using Mojolicious::Plugin::Model but I don't know why I can't access to the app.

Below simplified code

App

package App;
use Mojo::Base 'Mojolicious';

sub startup {
my $self = shift;
$self->plugin('Model');
$r->get('/')->to('Premium#create')
}


Controller

package App::Premium;
use Mojo::Base 'Mojolicious::Controller';
use App::Model::User;

sub create {
my $self = shift;

my $user = App::Model::User->new(name => 'John doe');
$user->create();
$self->render(text => 'Ok');
}


Model

package App::Model::User;
use Mojo::Base 'MojoX::Model';

sub create {
my $self = shift;

$self->app->mysql->db->query('...');
}


But the model can not access to app methods...

Answer

Controller:

$self->model('User')->create(name => 'John doe');

Model:

sub create {
    my ($self, %attrs) = @_;

    $self->app->mysql->db->query('...');
}
Comments