From 5366a89393539a1f633ad962d3b33a35f93ef752 Mon Sep 17 00:00:00 2001 From: Mirza Zeyrek Date: Mon, 27 Sep 2021 14:49:47 +0200 Subject: [PATCH 1/2] introduce saveVersion method to decouple validation logic and save version model logic. --- src/Mpociot/Versionable/VersionableTrait.php | 41 ++++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Mpociot/Versionable/VersionableTrait.php b/src/Mpociot/Versionable/VersionableTrait.php index 9be633a..0a07971 100644 --- a/src/Mpociot/Versionable/VersionableTrait.php +++ b/src/Mpociot/Versionable/VersionableTrait.php @@ -155,7 +155,7 @@ protected function versionablePreSave() } /** - * Save a new version. + * Save a new version if it is valid for saving. * @return void */ protected function versionablePostSave() @@ -168,25 +168,34 @@ protected function versionablePostSave() ( $this->versioningEnabled === true && !$this->updating && !is_null($this->versionableDirtyData) && count($this->versionableDirtyData)) ) { // Save a new version - $class = $this->getVersionClass(); - $version = new $class(); - $version->versionable_id = $this->getKey(); - $version->versionable_type = method_exists($this, 'getMorphClass') ? $this->getMorphClass() : get_class($this); - $version->user_id = $this->getAuthUserId(); - - $versionedHiddenFields = $this->versionedHiddenFields ?? []; - $this->makeVisible($versionedHiddenFields); - $version->model_data = serialize($this->attributesToArray()); - $this->makeHidden($versionedHiddenFields); + $this->saveVersion(); - if (!empty( $this->reason )) { - $version->reason = $this->reason; - } + $this->purgeOldVersions(); + } + } + + /** + * Save a new version. + * @return void + */ + public function saveVersion() + { + $class = $this->getVersionClass(); + $version = new $class(); + $version->versionable_id = $this->getKey(); + $version->versionable_type = method_exists($this, 'getMorphClass') ? $this->getMorphClass() : get_class($this); + $version->user_id = $this->getAuthUserId(); - $version->save(); + $versionedHiddenFields = $this->versionedHiddenFields ?? []; + $this->makeVisible($versionedHiddenFields); + $version->model_data = serialize($this->attributesToArray()); + $this->makeHidden($versionedHiddenFields); - $this->purgeOldVersions(); + if (!empty( $this->reason )) { + $version->reason = $this->reason; } + + $version->save(); } /** From c5e07cdde26d116ab5653d34d1ed417f60e081b3 Mon Sep 17 00:00:00 2001 From: Mirza Zeyrek Date: Mon, 27 Sep 2021 15:05:01 +0200 Subject: [PATCH 2/2] update readme with saveVersion method. --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 57f119d..14fd48d 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,20 @@ $user->update([ ]); ``` + + +### Create initial version for an existing model + +When you integrate versionable package in an existing project, you might want to create initial versions for the existing models +without making any actual changes. + +This can be achieved by using the `saveVersion` method on the versionable model. + +```php +$user = User::find(1); +$user->saveVersion(); +``` + ### Use different version table