Created
September 20, 2012 19:30
-
-
Save rinatio/3757866 to your computer and use it in GitHub Desktop.
ActiveRecord
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 | |
class ActiveRecord extends CActiveRecord | |
{ | |
public static function model($className = null) | |
{ | |
return parent::model($className ? : get_called_class()); | |
} | |
protected function beforeSave() | |
{ | |
if ($this->getIsNewRecord()) { | |
if (isset($this->getMetaData()->columns['create_date']) && !$this->create_date) | |
$this->create_date = new CDbExpression('NOW()'); | |
if (isset($this->getMetaData()->columns['create_user_id']) && !$this->create_user_id) | |
$this->create_user_id = Yii::app()->user->id; | |
} | |
if (isset($this->getMetaData()->columns['last_update_date'])) | |
$this->last_update_date = new CDbExpression('NOW()'); | |
if (isset($this->getMetaData()->columns['last_update_user_id'])) | |
$this->last_update_user_id = Yii::app()->user->id; | |
return parent::beforeSave(); | |
} | |
/* Trick to update active_yn instead of deleting records */ | |
// Todo: it would be better to override CDbCommandBuilder::createDeleteCommand | |
// but can't find the way to do that except using namespaces probably | |
function deleteByPk($pk, $condition = '', $params = array()) | |
{ | |
if (isset($this->getMetaData()->columns['active_yn'])) { | |
return $this->updateByPk($pk, array( | |
'active_yn' => false, | |
'last_update_date' => new CDbExpression('NOW()'), | |
'last_update_user_id' => Yii::app()->user->id, | |
), $condition, $params); | |
} else | |
return parent::deleteByPk($pk, $condition, $params); | |
} | |
function deleteAll($condition = '', $params = array()) | |
{ | |
if (isset($this->getMetaData()->columns['active_yn'])) { | |
return $this->updateAll(array( | |
'active_yn' => false, | |
'last_update_date' => new CDbExpression('NOW()'), | |
'last_update_user_id' => Yii::app()->user->id, | |
), $condition, $params); | |
} else | |
return parent::deleteAll($condition, $params); | |
} | |
function deleteAllByAttributes($attributes, $condition = '', $params = array()) | |
{ | |
if (isset($this->getMetaData()->columns['active_yn'])) { | |
$builder = $this->getCommandBuilder(); | |
$table = $this->getTableSchema(); | |
$criteria = $builder->createColumnCriteria($table, $attributes, $condition, $params); | |
$command = $builder->createUpdateCommand($table, array( | |
'active_yn' => false, | |
'last_update_date' => new CDbExpression('NOW()'), | |
'last_update_user_id' => Yii::app()->user->id, | |
), $criteria); | |
return $command->execute(); | |
} else | |
return parent::deleteAll($condition, $params); | |
} | |
public function defaultScope() | |
{ | |
if (isset($this->getMetaData()->columns['active_yn'])) { | |
$t = $this->getTableAlias(false, false); | |
return array('condition' => "$t.active_yn = 1"); | |
} else { | |
return parent::defaultScope(); | |
} | |
} | |
/* end of trick */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment