初版 2010-12-19 丸野
BMIのデータを、データベースを使って管理する
================================================================================
Symfony を使ったBMIアプリケーションで、データベースを使ってデータを管理する
モジュールを作る。
1. 入力データをデータベースを使って管理する(テーブル名はbmi)
列は
id int, auto increment
name varchar(50) 名前
height int 身長(cm)
weight int 体重(kg)
created_at
2. 画面はCRUDの画面を使う(bmiadminモジュールとする)
3. リストやブローズ画面では、BMI値もあわせて表示する
(以降オプション)
4. 入力データのチェックする
名前、身長(cm)、体重(kg)は、必須
身長、体重は、数値で、それぞれ300、500を上限とする
5. 編集、削除はログインが必要とする
6. ログアウトをどの画面からでもできるようにする
■ schema.yml
---
bmi:
id:
name: varchar(50)
height: integer
weight: integer
created_at:
---
■ テーブル/モデル、モジュールの作成
---
$ symfony propel-build-all
$ symfony propel-generate-crud bmi bmiadmin Bmi
---
■ モデルBmiクラス
---
height / 100, 2);
return $this->weight / $denominator;
}
}
---
■ テンプレート
□ listSuccess.php
---
BMI管理
ID |
名前 |
身長(cm) |
体重(kg) |
BMI |
登録日時 |
getId(), 'bmiadmin/show?id='.$bmi->getId()) ?> |
getName() ?> |
getHeight() ?> |
getWeight() ?> |
getBmi(), 1) ?> |
getCreatedAt('Y-m-d') ?> |
---
□ showSuccess.php
---
ID: |
getId() ?> |
名前: |
getName() ?> |
身長(cm): |
getHeight() ?> cm |
体重(kg): |
getWeight() ?> kg |
BMI: |
getBmi(), 1) ?> |
登録日時: |
getCreatedAt('Y-m-d') ?> |
getId()) ?>
---
□ editSuccess.php
---
名前: |
'30',
)) ?> |
身長: |
7,
)) ?> cm |
体重: |
7,
)) ?> kg |
hasErrors()) {
foreach ($sf_request->getErrors() as $error) {
echo $error . "
";
}
}
?>
getId()): ?>
getId(), 'post=true&confirm=本当に削除しますか?') ?>
getId()) ?>
---
■ バリデーター
□ update.yml
---
fields:
name:
required:
msg: "名前は必須です。"
height:
required:
msg: "身長は必須です。"
sfNumberValidator:
nan_error: "整数を入力してください。"
max: 300
max_error: "身長は300cm以内で入力してください。"
weight:
required:
msg: "体重は必須です。"
sfNumberValidator:
nan_error: "整数を入力してください。"
max: 500
max_error: "体重は500kg以内で入力してください。"
---
□ actions.class.php の追加関数のみ
---
public function handleErrorUpdate() {
if (!$this->getRequestParameter("id")) {
return $this->forward("bmiadmin", "create");
} else {
return $this->forward("bmiadmin", "edit");
}
}
---
■ セキュリティ
□ bmiadminモジュールのsecurity.yml
---
all:
is_secure: on
index:
is_secure: off
list:
is_secure: off
show:
is_secure: off
---
以上