++ed by:
1 non-PAUSE user
Author image Михаил Че
and 1 contributors

Доброго всем

DBIx::Mojo::Model

¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !

NAME

DBIx::Mojo::Model - base class for DBI models with templating statements by Mojo::Template.

SINOPSYS

Init once base singleton for process with dbh and (optional) template vars:

  use DBIx::Mojo::Model;
  DBIx::Mojo::Model->singleton(dbh=>$dbh, template_vars=>$t);

In child model must define SQL dict in __DATA__ of model package:

  package Model::Foo;
  use Mojo::Base 'DBIx::Mojo::Model';
  
  sub new {
    state $self = shift->SUPER::new(mt=>{tag_start=>'{%', tag_end=>'%}'}, @_);
  }
  
  sub foo {
    my $self = ref $_[0] ? shift : shift->new;
    $self->dbh->selectrow_hashref($self->sth('foo', where => 'where id=?',), undef, (shift));
  }
  
  __DATA__
  @@ foo?cached=1
  %# my foo statement with prepare_cached
  select *
  from foo
  {% $where %}
  ;

In controller:

  ...
  has model => sub { require Model::Foo; Model::Foo->new };
  
  sub actionFoo {
    my $c = shift;
    my $foo = $c->model->foo($c->param('id'));
    ...
  
  }

ATTRIBUTES

dbh

dict

DBIx::Mojo::Template object. If not defined then will auto create from __DATA__ current model package.

  Model::Foo->new(dict=>DBIx::Mojo::Template->new('Model::Foo', ...), ...)

mt

Hashref Mojo::Template object attributes. Will passed to Mojo::Template->new then dict auto create

template_vars

Hashref variables applies in statement templates.

dbi_cache_st

Boolean switch: 1(true) - use DBI caching ($dbh->prepare_cached) and 0(false) overvise this module caching.

The statement must has defined cached param:

  @@ foo query name?cached=1
  select ...

Defaults is true for save statement inside DBIx::Mojo::Statement object atribute sth.

METHODS

new

Define or copy/merge attributes from singleton.

singleton

Initialize default attributes for child model modules. Mostly dbh and template_vars

sth

This is main method.

First input arg is dict statement name, next args key => val are template variables. Return DBI prepared (cached if param 'cached' is true) statement.

Templates

Body of template statement get as:

  $mFoo->dict->{'foo'}->sql

Templates name can has additional params as ulr query:

  @@ foo.bar/baz?a=156&b=c
  ...

then model object the name of statement is url path and query is param:

  $mFoo->dict->{'foo.bar/baz'}->param->{b} # 'c'

AUTHOR

Михаил Че (Mikhail Che), <mche[-at-]cpan.org>

BUGS / CONTRIBUTING

Please report any bugs or feature requests at https://github.com/mche/Mojolicious-Plugin-RoutesAuthDBI/issues. Pull requests also welcome.

COPYRIGHT

Copyright 2016 Mikhail Che.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.