我是一位全新的Laravel开发人员,我使用VentureCraft可修改库来记录模型修订版。我需要记录多对多模型的修订版本,我的理解是Revisionable不支持这一点。我看过其他图书馆,他们似乎也没有。有没有办法可以做到这一点?即记录更改为没有模型类的数据透视表?
对于这个广泛的问题感到抱歉,但我已经陷入困境,并且真的不知道该怎么做。任何提示或有用的文档将不胜感激。
我想使用可修改的库,但实际上我只需要在修订表中添加一条记录,其中相应的数据透视表中的数据已更改,我不知道如何去做这样做。提前谢谢。
答案 0 :(得分:5)
虽然目前没有本地方式,但有几种选择。我在某些模型上做的事情,我需要将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;
}
然后稍后从我的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);
}
这不是最优雅的方式,但这是我第一次使用hacky选项。
话虽如此......在Revisionable v2.0 +中将会有内置的功能,正在进行中!