Lithium 0.9.9が出ていましたね!
http://rad-dev.org/lithium/wiki/blog/Lithium-0-9-9-99-Problems-But-a-Framework-Aint-One
でもこの記事はLithium 0.9.5を対象にしています。早くLithium 0.9.9にしたい!
というわけで前回のLithiumで作ってみたその3の続きです。
まず初めに結果はこれです。
http://sample.conol.jp/lithium/app_sample/sample1/posts
今回は投稿の更新用に/sample1/editを作ります。
Sample1のコントローラーに更新用のeditを追加します。
/sample1/edit/{:編集するデータのid}
というURLでアクセスされることを想定しています。
namespace app\controllers; use app\models\Sample1; class Sample1Controller extends \lithium\action\Controller { // 省略 public function edit() { if (!$this->request->id || !($col = Sample1::find($this->request->id))) $this->redirect(array('controller' => 'sample1')); if($this->request->data && isset($this->request->data['text'])) { // $this->request->idが有効、かつtextがpostされたなら $col->text = $this->request->data['text']; // 以下の処理は/sample1/addと同様 if($col->validates() && $col->save()) { $this->redirect(array('action' => 'success')); } else { $errors = $col->errors(); $this->set(array('form_data' => array( 'errors' => $errors['text'], 'text' => $col->text ))); } } else { // $this->request->idは有効だけどtextがpostされていなかったら(編集画面) $this->set(array('form_data' => array( 'text' => $col->text ))); } $this->render('form'); }
validatesからの処理は/sample1/addと同様です。ただその前にtextの値がpostされなかったら編集の画面、されたら保存の処理という分岐がしてあります。
この編集の処理なのですが、Lithiumのsaveでうまくアップデートが出来ない件についてで書いたようにlithium\data\source\MongoDb::update()に変更を加えた上での処理です。なので若干不安な感じも否めません… (´ω`)
まあ、とにかくこれでひと通りhttp://sample.conol.jp/lithium/app_sample/sample1/postsのような感じに動くようになっていると思います。
まとめ。
Lithiumで作ってみたその1
Lithiumで作ってみたその2
Lithiumで作ってみたその3
そして今まで散々
$this->render('テンプレート名');
と書いてきましたが、Lithium 0.9.9からはしっかり
$this->render(array('template' => 'テンプレート名'));
というように書く必要があるようです。キヲツケテ!