Created
January 15, 2012 03:16
-
-
Save jamband/1614119 to your computer and use it in GitHub Desktop.
Yii Framework: FlashBehavior r3
This file contains 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 | |
/** | |
* FlashBehavior class file. | |
*/ | |
class FlashBehavior extends CActiveRecordBehavior | |
{ | |
/** | |
* @var string key identifying the flash message | |
*/ | |
public $key = 'success'; | |
/** | |
* @var array flash message | |
*/ | |
public $message = array('insert', 'update', 'delete'); | |
/** | |
* @see CActiveRecord::afterSave() | |
*/ | |
public function afterSave($event) | |
{ | |
$action = $this->owner->isNewRecord ? 'insert' : 'update'; | |
$this->setFlashMessage($action); | |
} | |
/** | |
* @see CActiveRecord::afterDelete() | |
*/ | |
public function afterDelete($event) | |
{ | |
$this->setFlashMessage('delete'); | |
} | |
/** | |
* Set a flash message. | |
* @param mixed $action | |
*/ | |
protected function setFlashMessage($action) | |
{ | |
if (!Yii::app() instanceof CConsoleApplication) | |
Yii::app()->user->setFlash($this->key, $this->message[$action]); | |
} | |
} |
This file contains 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 | |
/** | |
* This is the model class for table "hoge". | |
*/ | |
class Hoge extends ActiveRecord | |
{ | |
... | |
/** | |
* @see CActiveRecord::behaviors() | |
*/ | |
public function behaviors() | |
{ | |
return array( | |
'FlashBehavior' => array( | |
'class' => 'application.components.FlashBehavior', | |
'message' => array( | |
'insert' => 'Hogeの追加が完了いたしました', | |
'update' => 'Hogeの更新が完了いたしました', | |
'delete' => 'Hogeの削除が完了いたしました', | |
), | |
), | |
); | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment