1Maypole::Manual::Model(U3s)er Contributed Perl DocumentatMiaoynpole::Manual::Model(3)
2
3
4

NAME

6       Maypole::Manual::Model - Maypole Model Classes
7

DESCRIPTION

9       Maypole's model classes provide an interface to your data store.  In
10       principle Maypole can connect to pretty much any data source, but the
11       default model is based on the popular Class::DBI object interface that
12       uses the near-universal DBI Perl interface to databases.
13
14       Maypole::Model::CDBI
15
16       Maypole model classes are pretty evil. The very first thing a Maypole
17       model class will do in a Maypole application is to cause a load of ta‐
18       ble-based classes to come into being, and then assimilate them.
19
20       What I mean by this is that when you set up a Maypole application, in
21       your driver class, you'll say something like this:
22
23           BeerDB->setup("dbi:mysql:beerdb");
24
25       "setup" is a Maypole method, and it hands its parameter to the model
26       class. In our case, the argument is a DBI connect string, because
27       that's what "Maypole::Model::CDBI", the "Class::DBI"-based model
28       expects. "Maypole::Model::CDBI" has a method called "setup_database"
29       that creates all the "Class::DBI" table classes after connecting to the
30       database with that connect string. It does this by using
31       "Class::DBI::Loader", a utility module which asks a database about its
32       schema and sets up classes such as "BeerDB::Beer" to inherit from
33       "Class::DBI".
34
35       Now it gets interesting. The names of these classes are stashed away in
36       the application's configuration, and then Maypole forcibly has these
37       classes inherit from the model class. Our "BeerDB::Beer" now inherits
38       both from "Class::DBI" and "Maypole::Model::CDBI".
39
40       This is useful for two reasons. The first reason is that "May‐
41       pole::Model::CDBI" is stuffed full of "Class::DBI" goodies that make
42       writing Maypole applications a lot easier:
43
44           package Maypole::Model::CDBI;
45           use base qw(Maypole::Model::Base Class::DBI);
46           use Maypole::Model::CDBI::AsForm;
47           use Class::DBI::FromCGI;  # probabyly broken .
48           use Class::DBI::Loader;
49           use Class::DBI::AbstractSearch;
50           use Class::DBI::Plugin::RetrieveAll;
51           use Class::DBI::Pager;
52
53       We'll meet most of these goodies in the Standard Templates and Actions
54       chapter, where we explain how "Maypole::Model::CDBI" works.
55
56       The second reason why we want our table classes to inherit from "May‐
57       pole::Model::CDBI" is because it provides a useful set of default
58       actions. So what's an action, and why are they useful?
59
60       Extending a model class with actions
61
62       Maypole operates primarily by turning URLs into method calls on a model
63       class. All that the model stage of Maypole's operation does, when it
64       comes down to it, is maintain a mapping of tables to classes, and
65       despatch a HTTP request to a method call on the relevant table class.
66       This means that if you request a URL such as
67
68           http://localhost/beerdb/brewery/delete/20
69
70       Maypole's first stage of operation is to turn that into "BeerDB::Brew‐
71       ery->delete(20)". Now, it's slightly more complex than that. Firstly
72       because it doesn't actually pass the parameter 20, but it passes an
73       object representing row 20 in the database, but we can gloss over that
74       for the second. No, the real issue is that Maypole does not allow you
75       to call just any method in the table class; that would be somewhat
76       insecure.
77
78       Instead, Maypole makes a distinction between the kind of methods that
79       only the class itself and other Perl code can call, and the kind of
80       methods that anyone can call from a URL. This latter set of methods are
81       called exported methods, and exporting is done by means of Perl
82       attributes. You define a method to be exported like so:
83
84           sub drink :Exported {
85
86       This will allow the user to access "/beerdb/beer/drink" over the web.
87       An exported method accompanied with a template to render its output is
88       sometimes called an action.
89
90       Maypole model classes like "Maypole::Model::CDBI" come with a rela‐
91       tively handy set of actions which are all you need to set up a CRUD
92       (Create, Read, Update, Delete) database front-end: viewing a row in a
93       database, editing it, adding a new one, deleting, and so on. The most
94       important thing about Maypole, though, is that it doesn't stop there.
95       You can add your own.
96
97       For instance, in our beer database application, we could create a
98       "BeerDB::Beer" package, and put some additional actions in there.
99
100           package BeerDB::Beer;
101           sub top_five :Exported {
102               my ($class, $r) = @_;
103               $r->objects([ ($r->retrieve_all_sorted_by("score"))[-5..-1] ]);
104           }
105
106       Our action is called as a class method with the Maypole request object.
107       It uses the "Class::DBI::Plugin::RetrieveAll" module's
108       "retrieve_all_sorted_by" mixin to get the top five scoring beers, and
109       puts these in the "objects" slot of the request of object. Next, the
110       view class will come along and process the "top_five" template with
111       these five beers.
112
113       We'll look more at how to put together actions in the Standard Tem‐
114       plates and Actions chapter and our case studies.
115
116       Links
117
118       Contents, Next Maypole View Classes, Previous Introduction to Maypole
119
120
121
122perl v5.8.8                       2005-11-23         Maypole::Manual::Model(3)
Impressum