Konstrukt::Doc::Tutorial::Plugin::Note::DBI - Using Perl DBI to store your data in a database tutorial.
This tutorial will teach you how to use DBI to store your data.
Additionally we will extend the plugin to allow the creation of multiple notes.
Note: This tutorial builds up on the results of the previous tutorial.
CONVENTIONS AND SETTINGS
By convention the backends for each plugin are implemented as a separate plugin module. This way the backends are easily exchangeable by setting up a different backend in the konstrukt.settings.
Thus the main plugin will have a new setting
note/backend DBI #default backend: DBI
which will be initialized in the init method, where the specified backend will also be loaded. So we add those lines to the init method:
$Konstrukt::Settings->default("note/backend" => 'DBI');
$self->{backend} = use_plugin "note::" . $Konstrukt::Settings->get('note/backend') or return undef;
THE DBI BACKEND PLUGIN
Backend Settings
Create a new directory /path/to/your/site/lib/Konstrukt/Plugin/note and inside this directory create a file named DBI.pm.
The backend plugin itself should also be configurable with these settings:
note/backend/DBI/source dbi:mysql:database:host
note/backend/DBI/user user
note/backend/DBI/pass pass
If no database settings are set, the website-wide defaults of the Konstrukt DBI helper will be used automatically.
You should specify the website-wide database defaults in your konstrukt.settings:
dbi/source dbi:mysql:database:host
dbi/user user
dbi/pass pass
It it doesn't exist, you have to create this database now. An example query for this can be found in the blog tutorial.
Skeleton
The skeleton for the backend module including the init method will look like this:
package Konstrukt::Plugin::note::DBI;
use strict;
use warnings;
use base 'Konstrukt::Plugin';
sub init {
my ($self) = @_;
#get connection settings
my $db_source = $Konstrukt::Settings->get('blog/backend/DBI/source');
my $db_user = $Konstrukt::Settings->get('blog/backend/DBI/user');
my $db_pass = $Konstrukt::Settings->get('blog/backend/DBI/pass');
#save settings in a handy hashref for later use
$self->{db_settings} = [$db_source, $db_user, $db_pass];
return 1;
}
1;
Product's homepage
Requirements:
· Perl