Skip to content

Instantly share code, notes, and snippets.

@hugodotlau
Forked from jamband/FlashBehavior.php
Created September 27, 2012 13:42
Show Gist options
  • Save hugodotlau/3794071 to your computer and use it in GitHub Desktop.
Save hugodotlau/3794071 to your computer and use it in GitHub Desktop.
Yii Framework: FlashBehavior r3
<?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]);
}
}
<?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