Skip to content

Instantly share code, notes, and snippets.

@jamband
Created December 3, 2011 04:56
Show Gist options
  • Select an option

  • Save jamband/1426095 to your computer and use it in GitHub Desktop.

Select an option

Save jamband/1426095 to your computer and use it in GitHub Desktop.
<?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