Skip to content

Instantly share code, notes, and snippets.

@ModernTimes
Created September 4, 2012 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ModernTimes/3615263 to your computer and use it in GitHub Desktop.
Save ModernTimes/3615263 to your computer and use it in GitHub Desktop.
LazySaverBehavior: prevents save() command from being executed if no attributes have changed
<?php
Yii::import('ext.AttributesBackupBehavior');
/**
* Prevent save() command from being executed if no attributes have changed
* The AttributesBackupBehavior class is available here:
* http://www.yiiframework.com/extension/attributesbackupbehavior/
*/
class LazySaverBehavior extends AttributesBackupBehavior {
/**
* Prevent save() command from being executed if no attributes have changed
* @param CModelEvent $event
*/
public function beforeSave($event) {
$owner = $this->getOwner();
if(!$owner->getIsNewRecord() && !$this->attributesChanged()) {
$event->isValid = false;
}
parent::beforeSave($event);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment