Last active
December 12, 2015 12:39
-
-
Save grantm/4773217 to your computer and use it in GitHub Desktop.
Syntactic sugar for DBIx::Class stuff in Dancer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In my Dancer app I load a 'user' record like this: | |
my $user = User->find( param('id') ); # usually wrapped in a check that the record was found | |
# There are a number of 'moving parts' to allow this to work. | |
# Near the top of my Dancer code I have this: | |
use MyApp::Helpers; | |
sub User { model('AppUser'); } | |
# So the User->find line is calling the 'find' method on the object returned by the User function. | |
# I chose to give the sub a capital 'U' because I use it a bit like a class. I also have similar | |
# 1-line subs for the other tables I need to access. | |
# The User sub uses the 'model' helper which came from MyApp/Helper.pm and looks like this: | |
use Dancer::Plugin::DBIC 'schema'; | |
sub model { | |
return schema->resultset( shift ); | |
} | |
# The 'schema' function is provided by the Dancer::Plugin::DBIC module. The plugin gets the | |
# database connection details from the Dancer config file. These details include a DSN and also: | |
schema_class: MyApp::Schema | |
# The MyApp/Schema.pm file is standard DBIx::Class stuff and may for example be generated by | |
# DBIx::Class::Schema::Loader. This is where the MyApp::Schema::Result::AppUser class | |
# is mapped to the 'app_user' table in the DB. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment