[dancer-users] Trouble with DBIC in Plugins

Andrew Beverley andy at andybev.com
Sat May 2 10:47:52 BST 2015


On Sat, 2015-05-02 at 12:50 +1000, Matthew Mallard wrote:
> >> I’m getting a bit confused with plugins.  I’m sure that I was able to
> >> use the DBIC Plugin at one point from one of my plugins, but I can’t
> >> seem to get it to  work in 0.159002 or 0.160000.  DBIC seems to be
> >> okay outside of plugins.
> > 
> > Firstly, there is change going on in the world of plugins, which I
> > suspect will fix this, but I haven't read up on the full details myself:
> > 
> > http://lists.preshweb.co.uk/pipermail/dancer-users/2015-April/004512.html
> > 
> >> * how do I get DBIC to print the SQL it’s executing so I can double
> >> check it’s what I’m expecting?
> >> * am I doing something else wrong in the way I’m trying to use DBIC?
> > 
> > This is a bit more tricky. With the current plugin architecture,
> > "schema" is not available in the DSL when you call it from another
> > plugin as you are doing. You can workaround this by capturing it during
> > plugin initialisation, as long as you load the DBIC plugin before yours.
> > See Plugin::Auth::Extensible::Provider::DBIC for an example:
>
> So do I create a sub in my custom plugin along these lines:
> sub new {
>     my ($class, $dsl) = @_;
> 
>     # Grab a handle to the Plugin::DBIC schema
>     die "No schema method in app. Did you load DBIC::Plugin::DBIC before My::Custom::Module?"
>         unless $dsl->can('schema');
>     my $schema = $dsl->schema;

Yes, similar to that, but using on_plugin_import. See this example:

https://github.com/PerlDancer/Dancer2-Plugin-Auth-Extensible/blob/de84a4a5baf5b56a83906355de075678c485fc51/lib/Dancer2/Plugin/Auth/Extensible.pm#L1158

>     my $self = {
>         dsl_local      => $dsl,
>         schema         => $schema,
>     };
>     return bless $self => $class;

I don't think you need to bless your class, as it's already blessed. You
just need to store the schema in a global in your module, a bit like
$settings here:

https://github.com/PerlDancer/Dancer2-Plugin-Auth-Extensible/blob/de84a4a5baf5b56a83906355de075678c485fc51/lib/Dancer2/Plugin/Auth/Extensible.pm#L13

> Then in my routes module I call plugins in this order:
> use Dancer2::Plugin::DBIC
> use My::Custom::Module

Yes.

> How does 'new' get called and what do I use to reference the schema in my custom plugin.

See above.

> Let me know if there is some other background reading I should be doing
> for how this work,

To be honest, it's a dirty hack, so you won't find any documentation on
it. You might be better waiting for the new plugin architecture if you
can.

Andy




More information about the dancer-users mailing list