
下記フォームに必要事項を入力後、確認ボタンを押してください。
今回は前回から引き続きModelクラスを使用してリクエストオブジェクトを使用してフォームからデータベースをアクセスするフォームの開発を進めます。
[request.pm]
sub find_form :Local { }
sub findresult_form:Local {
my ( $self, $c ) = @_;
$c->stash->{book}=$c->model('BookDB::Book')->find($c->request->body_params->{'id'});
#bookのidをキーにBOOKテーブルから該当するレコードを取得
$c->stash->{message} =decode('sjis','IDからデータを検索しました。');
}
A 入力画面[find_form.tt]の作成
データ入力画面を作成します。
[find_form.tt] <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="stylesheet.css" > <meta charset="UTF-8"> <title>リクエストオブジェクト</title> </head> <body> <h1>ファインドフォーム―データベースアクセス―</h1> <p> <form method="post" action="/request/findresult_form"> <table><tr><td style="width:50px;">ID:</td><td><input type="text" name="id" size="3" maxlength="255"/></td></tr></table> <input type="submit" value="検 索"/> </form> </p> </body> </html>B 出力画面[searchresult_form.tt]の作成 データ抽出結果を表示する画面を作成します。
[findresult_form.tt]
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="stylesheet.css" >
<meta charset="UTF-8">
<title>リクエストオブジェクト</title>
</head>
<body>
<h1>ファインドリザルトフォーム―データベースアクセス―</h1>
<p>
[% message | html %]<br>
<table>
[%- FOREACH item = book -%]
<tr><td>ID:</td><td style="width:350px;">[% item.id | html %]</td></tr>
<tr><td>タイトル:</td><td>[% item.title | html %]</td></tr>
<tr><td>Review:</td><td>[% item.review | html %]</td></tr>
<tr><td>Rating:</td><td>[% item.rating | html %]</td></tr>
<tr><td>Price:</td><td>[% item.price | html %]</td></tr>
<tr><td>Published:</td><td>[% item.published | html %]</td></tr>
[% END %]
</table><br>
<form><input type="button" value="前の画面に戻る" onClick="location.replace('find_form')"
style="width:180px;"></form></p>
</body>
</html>
http://localhost:3000/request/find_formのアドレスを開き、データ検索画面からIDを入力して該当のデータを抽出できれば成功です。
[request.pm]
sub search_form :Local { }
sub searchtitleresult_form :Local {
my ( $self, $c ) = @_;
#book_title列をキーにbookテーブルを検索
$c->stash->{book}= [$c->model('BookDB::Book')->search({title =>$c->request->body_params->{'title'}})];
$c->stash->{message} =decode('sjis','タイトルからデータを検索しました。');
}
sub searchwhenresult_form :Local {
my ( $self, $c ) = @_;
#book_publishedからbookテーブルの該当データを検索
$c->stash->{book}= [$c->model('BookDB::Book')->search({published =>{ '>=' =>$c->request->body_params->{'when'}}})];
$c->stash->{message} =decode('sjis','出版日からデータを検索しました。');
}
A 入力画面[search_form.tt]の作成
データ入力画面を作成します。
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="stylesheet.css" > <meta charset="UTF-8"> <title>リクエストオブジェクト</title> </head> <body> <h1>サーチフォーム―データベースアクセス―</h1> <p> <form method="post" action="/request/searchtitleresult_form"> <table><tr><td style="width:260px;">Bookタイトル:</td><td><input type="text" name="title" size="30" maxlength="255"/></td></tr></table><br> <input type="submit" value="検 索" style="width:100px;"/> </form> </p><br> <p> <form method="post" action="/request/searchwhenresult_form"> <table><tr><td style="width:260px;">この時期より前の出版物を検索:</td> <td><input type="text" name="when" size="10" maxlength="255"/></td></tr></table> <span style="color:red;font-size:12px;">※ <****-**-**>の形式で入力してください。</span><br> <input type="submit" value="検 索" style="width:100px;"/> </form> </p> </body> </html>B 出力画面[searchtitleresult_form.tt][searchwhenresult_form.tt]の作成 出力結果を表示する画面を作成します。コードは同一でファイル名は[searchtitleresult_form.tt][searchwhenresult_form.tt]の2ファイル用意してください。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="stylesheet.css" >
<meta charset="UTF-8">
<title>リクエストオブジェクト</title>
</head>
<body>
<h1>サーチリザルトフォーム―データベースアクセス―</h1>
<p>
[% message | html %]<br>
<table>
[%- FOREACH item = book -%]
<tr><td>ID:</td><td style="width:350px;">[% item.id | html %]</td></tr>
<tr><td>タイトル:</td><td>[% item.title | html %]</td></tr>
<tr><td>Review:</td><td>[% item.review | html %]</td></tr>
<tr><td>Rating:</td><td>[% item.rating | html %]</td></tr>
<tr><td>Price:</td><td>[% item.price | html %]</td></tr>
<tr><td>Published:</td><td>[% item.published | html %]</td></tr>
[% END %]
</table><br>
<form><input type="button" value="前の画面に戻る" onClick="location.replace('search_form')" style="width:180px;"></form></p>
</body>
</html>
ファイルの設置を完了しましたら[http://localhost:3000/request/search_form]のアドレスを入力してタイトル若しくは出版日を入力して検索してください。
該当するデータが表示すれば成功です。
sub find_or_new_entry_form :Local { }
sub find_or_new_result_form :Local {
my ( $self, $c ) = @_;
my $book = $c->model('BookDB::Book')->find_or_new({
id=>$c->request->body_params->{'id'},
title=>$c->request->body_params->{'title'},
review=>$c->request->body_params->{'review'},
rating=>$c->request->body_params->{'rating'},
price=>$c->request->body_params->{'price'},
published =>$c->request->body_params->{'published'}
});
#レコードがすでに存在していた場合はエラーメッセージを表示
if($book->in_storage) {
$c->stash->{message} =decode('sjis','同一のキーでデータが存在します');
$c->stash->{id} = $c->request->body_params->{'id'};
$c->stash->{title} = $c->request->body_params->{'title'};
$c->stash->{review} = $c->request->body_params->{'review'};
$c->stash->{rating} = $c->request->body_params->{'rating'};
$c->stash->{price} = $c->request->body_params->{'price'};
$c->stash->{published} = $c->request->body_params->{'published'};
} else {
#レコードが存在しない場合は新規に登録
$book->insert();
$c->stash->{message} =decode('sjis','データを新規に登録しました。');
$c->stash->{id} = $c->request->body_params->{'id'};
$c->stash->{title} = $c->request->body_params->{'title'};
$c->stash->{review} = $c->request->body_params->{'review'};
$c->stash->{rating} = $c->request->body_params->{'rating'};
$c->stash->{price} = $c->request->body_params->{'price'};
$c->stash->{published} = $c->request->body_params->{'published'};
}
}
A 入力画面[find_or_new_entry_form.tt]の作成
データ入力画面を作成します。
[find_or_new_entry_form.tt] <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="stylesheet.css" > <meta charset="UTF-8"> <title>リクエストオブジェクト</title> </head> <body> <h1>[find_or_new_result]エントリーフォーム―データベースアクセス―</h1> <p> <form method="post" action="/request/find_or_new_result_form"> <table><tr><td style="width:150px;">ID:</td><td><input type="text" name="id" size="4" maxlength="255"/></td></tr> <tr><td>タイトル:</td><td><input type="text" name="title" size="40" maxlength="255"/></td></tr> <tr><td>レビュー:</td><td><input type="text" name="review" size="80" maxlength="255"/></td></tr> <tr><td>プライス:</td><td><input type="text" name="price" size="10" maxlength="255"/></td></tr> <tr><td>レーティング:</td><td><input type="text" name="rating" size="20" maxlength="255"/></td></tr> <tr><td>出版日:</td><td><input type="text" name="published" size="10" maxlength="255"/></td></tr></table> <input type="submit" value="送 信"/> </form> </p> </body> </html>B 出力画面[find_or_new_result_form.tt]の作成 データ抽出結果を表示する画面を作成します。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="stylesheet.css" >
<meta charset="UTF-8">
<title>リクエストオブジェクト</title>
</head>
<body>
<h1>リクエストフォーム―データベースアクセス―</h1>
<p>
[% message | html %]<br>
<ul>
<li>ID:[% id | html %]</li>
<li>title:[% title | html %]</li>
<li>Review:[% review | html %]</li>
<li>Rating:[% rating | html %]</li>
<li>Price:[% price | html %]</li>
<li>Published:[% published | html %]</li></ul>
</p>
<form><input type="button" value="前の画面に戻る" onClick="location.replace('find_or_new_entry_form')" style="width:180px;"></form>
</body>
</html>
開発サーバーを立ち上げ[http://localhost:3000/request/find_or_new_entry_form]を開いてエントリーフォームからデータを入力してください。[find_or_new_result_form]ページでデータ登録もしくはエラーメッセージが表示されれば成功です。
sub delete_form :Local { }
sub deleteresult_form:Local {
my ( $self, $c ) = @_;
#bookのidをキーにBOOKテーブルから該当するレコードを削除
my $result = $c->model('BookDB::Book')->search({id => $c->request->body_params->{'id'}})->delete();
$c->stash->{message} =decode('sjis','該当IDを削除しました。');
}
A 入力画面[delete_form.tt]の作成
データ入力画面を作成します。
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="stylesheet.css" > <meta charset="UTF-8"> <title>リクエストオブジェクト</title> </head> <body> <h1>Search Form―データベースアクセス―</h1> <p> <form method="post" action="/request/deleteresult_form"> <table><tr><td style="width:50px;">ID:</td><td><input type="text" name="id" size="3" maxlength="255"/></td></tr></table> <input type="submit" value="削 除"/> </form> </p> </body> </html>B 出力画面[deleteresult_form.tt]の作成 データ抽出結果を表示する画面を作成します。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="stylesheet.css" >
<meta charset="UTF-8">
<title>リクエストオブジェクト</title>
</head>
<body>
<h1>Delete Result Form―データベースアクセス―</h1>
<p>
[% message | html %]<br>
<table>
[%- FOREACH item = book -%]
<tr><td>ID:</td><td style="width:350px;">[% item.id | html %]</td></tr>
<tr><td>タイトル:</td><td>[% item.title | html %]</td></tr>
<tr><td>Review:</td><td>[% item.review | html %]</td></tr>
<tr><td>Rating:</td><td>[% item.rating | html %]</td></tr>
<tr><td>Price:</td><td>[% item.price | html %]</td></tr>
<tr><td>Published:</td><td>[% item.published | html %]</td></tr>
[% END %]
</table><br>
<form><input type="button" value="前の画面に戻る" onClick="location.replace('delete_form')" style="width:180px;"></form></p>
</body>
</html>
開発サーバーを立ち上げ[http://localhost:3000/request/delete_form]を開いてエントリーフォームから削除するデータのIDを入力してください。[deleteresult_form]ページでデータの削除が実行されていれば成功です。