Skip to content

Instantly share code, notes, and snippets.

@cornernote
Last active August 29, 2015 14:23
Show Gist options
  • Save cornernote/6a6629117b47172b30d1 to your computer and use it in GitHub Desktop.
Save cornernote/6a6629117b47172b30d1 to your computer and use it in GitHub Desktop.
<?php
namespace bedezign\yii2\audit\components;
use bedezign\yii2\audit\models\AuditTrail;
use yii\base\Component;
use yii\db\ActiveRecord;
/**
* Version
* @package bedezign\yii2\audit
*/
class Version extends Component
{
/**
* Get all versions of the model.
*
* @param $class
* @param $id
* @return array
*/
public static function versions($class, $id)
{
/** @var AuditTrail[] $trails */
$trails = AuditTrail::find()
->andWhere(['model' => $class, 'model_id' => $id])
->all();
$versions = [];
foreach ($trails as $trail) {
$versions[$trail->entry_id][$trail->field] = $trail->new_value;
}
return $versions;
}
/**
* Find a model from a version.
*
* @param $class
* @param $id
* @param $version
* @return ActiveRecord
*/
public static function find($class, $id, $version)
{
/** @var ActiveRecord $model */
$model = call_user_func_array([$class, 'findOne'], [$id]);
if (!$model) {
// if it has been deleted, load a new one
$model = new $class;
}
foreach ($model->attributes() as $attribute) {
$model->setAttribute($attribute, self::findAttribute($class, $id, $attribute, $version));
}
return $model;
}
/**
* Find an attribute from a version.
*
* @param $class
* @param $id
* @param $attribute
* @param $version
* @return null|integer|float|string
*/
public static function findAttribute($class, $id, $attribute, $version)
{
/** @var AuditTrail $trail */
$trail = AuditTrail::find()
->andWhere(['model' => $class, 'model_id' => $id, 'field' => $attribute])
->andWhere(['<=', 'entry_id', $version])
->orderBy(['id' => SORT_DESC])
->one();
return $trail ? $trail->new_value : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment