CakePHPで家計簿WEBアプリを作る ブログチュートリアルのおさらい
purchases_controller.phpにデータの登録(add function)、編集(edit function)、削除(delete function)
ここはこれと言って新しいことが無いので、ソースを紹介するだけで終わります
function add() {
if (!empty($this->data)) {
if ($this->Purchase->save($this->data)) {
$this->redirect('/purchases/');//情報をPOSTしてからの画面遷移
}
}
}
/*
$id = nullとは、以下URLでアクセスした時の、
http://example.com/purchase/edit/○○
○○の部分のこと。GETパラメータと役割は一緒と思ってください
*/
function edit($id = null) {
$this->Purchase->id = $id;
if (empty($this->data)) {
$this->data = $this->Purchase->read();
} else {
if ($this->Purchase->save($this->data['Purchase'])) {
$this->redirect('/purchases/');//情報をPOSTしてからの画面遷移
}
}
}
//データの削除 View無し
function delete($id) {
$this->Purchase->delete($id);
$this->redirect('/purchases/');//データ削除してからの画面遷移
}