Created
December 3, 2011 04:56
-
-
Save jamband/1426095 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class UpdateAction extends CAction | |
| { | |
| /** | |
| * リダイレクト先 | |
| */ | |
| public $redirect = 'view'; | |
| public function run($id) | |
| { | |
| $c = $this->getController(); // $cは各コントローラ内で使う$thisと同じ | |
| $m = ucfirst($c->getId()); //$mは各コントローラ内で使うモデル名 | |
| // 以下はコントローラで書くactionUpdateと似たり寄ったりです | |
| $model = $m::model()->findByPk($id); | |
| if (isset($_POST[$m])) | |
| { | |
| $model->attributes = $_POST[$m]; | |
| if ($model->save()) | |
| { | |
| // リダイレクト先を調整できるようにしている ( デフォルトは http://example/index.php/view/id/$id ) | |
| if ($this->redirect === 'view') | |
| $c->redirect(array($this->redirect, 'id' => $model->id)); | |
| else | |
| $c->redirect(array($this->redirect)); | |
| } | |
| } | |
| $c->render('_form', array('model' => $model)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment