bri*_*r82 2 php laravel eloquent revisionable
我是一个全新的Laravel开发人员,我正在使用VentureCraft可修改库来记录模型修订版.我需要记录多对多模型的修订版本,我的理解是Revisionable不支持这一点.我看过其他图书馆,他们似乎也没有.有没有办法可以做到这一点?即日志更改到没有模型类的数据透视表?
对不起这个广泛的问题,但是我被困住了,不知道该怎么做.任何提示或有用的文档将不胜感激.
我想使用可修改的库,但实际上我只需要在修订表中添加一条记录,其中相应数据透视表中的数据已更改,我不知道如何执行此操作.提前致谢.
小智 6
虽然目前没有本地方式,但有几种选择.我在某些模型上做的事情,我需要将ModelB的保存附加到ModelA(或任何场景),这是在类中(或在helpers.php中使用)实现的全局函数:
/**
* Creates a revision record.
*
* @param object $obj
* @param string $key
* @param mixed $old
* @param mixed $new
*
* @return bool
*/
public static function createRevisionRecord($obj, $key, $old = null, $new = null)
{
if (gettype($obj) != 'object') {
return false;
}
$revisions = [
[
'revisionable_type' => get_class($obj),
'revisionable_id' => $obj->getKey(),
'key' => $key,
'old_value' => $old,
'new_value' => $new,
'user_id' => vms_user('id'),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
]
];
$revision = new \Venturecraft\Revisionable\Revision;
\DB::table($revision->getTable())->insert($revisions);
return true;
}
Run Code Online (Sandbox Code Playgroud)
然后MyHelperClass::createRevisionRecord()从我的save()钩子里打电话.
public function save(array $options = [])
{
// $old = $this->getOriginal('key');
// $new = $this->key;
// Let's say this is linked to \App\SomethingElse via 'something_id'
MyHelperClass::createRevisionRecord(\App\SomethingElse::find($this->something_id), 'custom_field', $old, $new);
// Do actual save.
return parent::save($options);
}
Run Code Online (Sandbox Code Playgroud)
这不是最优雅的方式,但这是我第一次使用hacky选项.
话虽如此..在Revisionable v2.0 +中将有内置的功能正在进行中!
| 归档时间: |
|
| 查看次数: |
1096 次 |
| 最近记录: |