This extension will allow models to be backed up by an audit trail. It also creates the functionality of nested transactions (MySQL tested).
Resources ¶
Documentation ¶
Requirements ¶
- Yii 1.0 or above
Installation ¶
- Extract the release file under
protected/extensions
(usually I use components, but extensions should be fine) - File list: AuditedCActiveRecord --- extend from this class if you want the subclass to be backed by the audit trail AuditTrail --- the model describing the audit trail table AuditTrailSequenceNumber --- the model describing the audit trail sequence table CustomCActiveRecord --- the base class for all models NestedCDbConnection --- creates/supports nested transactions NestedTransactionPDO --- creates/supports nested transactions
Config ¶
- Modify config/main.php -> db array to use class=>NestedCDbConnection
- Import audit_schema.sql (MySQL)
Usage ¶
For the required model, extend from AuditedCActiveRecord. Save/delete as per normal. If transactions are desired, use transactedSave/transactedDelete.
Classes which want to use transatedSave/transactedDelete must extend from CustomCActiveRecord.
Read the comments in the classes for more information!
Change Log ¶
July 1, 2010 ¶
- Initial release.
transactions not rollback
$model=new Tsql;
for($i=14;$i>10;$i--){ $model->id=$i; $model->uo=$i; $model->po=$i; $model->transactedSave(); echo $model->uo.'='.$model->po; }
如上列,假如我数据中有id为11的记录,则报错,最后一条成功数据id为12的记录,不会rollback,其他的id为14,15的两条记录rollback成功
如果有两个以上语句需要事物处理如何做?
以下语句如何写?
protected function afterSave()
{
if(!$this->isNewRecord) $this->dbConnection->createCommand( 'DELETE FROM PostTag WHERE postId='.$this->id)->execute(); foreach($this->getTagArray() as $name) { if(($tag=Tag::model()->findByAttributes(array('name'=>$name)))===null) { $tag=new Tag(array('name'=>$name)); $tag->save(); } $this->dbConnection->createCommand( "INSERT INTO PostTag (postId, tagId) VALUES ({$this->id},{$tag->id})")->execute(); }
}
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.