Hello folks,

My name is Alceu and I'm giving baby steps with Mojolicious.

I just got an helping hand in the IRC channel to make the code below to 
work, but I would like to understand better the details.

When I using "bare" DBIx::Connector, right after I created a new instance 
of it, it would go straight to the DESTROY block... and all I would get was 
a empty reference.

After adding it as a helper ("helper conn" below), it worked as expected.

Why is it required to use a helper for that? I wasn't able to find any 
documentation about it.

Considering that I will need to use this connection for my Models, what is 
the best practice to use it?

The code follows below... thanks!

use Mojolicious::Lite;
use Mojolicious::Validator;
use Set::Tiny 0.04;
use File::Spec::Functions qw( catdir catfile );
use File::Share 0.25 'dist_dir';
use Mojolicious::Plugin::Config;
use Carp;
use DBIx::Connector;
use Data::Dumper;

my $config = {
    dsn  => "dbi:mysql:metabase;host=127.0.0.1;port=30306",
    user => 'cpantesters',
    pass => '',
};

helper conn => sub {
    state $conn = DBIx::Connector->new( $config->{dsn}, $config->{user},
        $config->{pass}, { RaiseError => 1, AutoCommit => 1 } );
    $conn->mode('fixup');
    return $conn;
};

get '/' => sub {
    my $c   = shift;
    my $ref = $c->conn->run(
        fixup => sub {
            my $query = q{SELECT * FROM cpanstats.osname};
            my $sth   = $_->prepare($query);
            $sth->execute();
            return $sth->fetchall_arrayref;
        }
    );

    $c->render( text => Dumper($ref) );
};

app->start;


-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to