Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model with existing record but no version does not create a version on save() #94

Open
JurjenRoels opened this issue Aug 26, 2022 · 5 comments

Comments

@JurjenRoels
Copy link

Hi,

I have a database with existing records and we add the VersionableTrait.

I would expect to get a version if we save an existing record?

However I get this with tinker:

$z->versions
=> Illuminate\Database\Eloquent\Collection {#4160
all: [],
}

$z->save()
=> true

$z->versions
=> Illuminate\Database\Eloquent\Collection {#4160
all: [],
}

DD of relevant information on record:

+exists: true
+wasRecentlyCreated: false
-updating: true
-versionableDirtyData: []
-reason: null
#versioningEnabled: true

How can I create a version on this record

@nonoesp
Copy link
Collaborator

nonoesp commented Sep 6, 2022

Hi @JurjenRoels – according to this method, versions are only saved on initial creation and updates. But you're saving without making any modifications to the model.

I've also missed that functionality when adding this library to models that didn't have it before with a populated database. One thing you could do is recreate all models from scratch with existing data, but I know that's not the best option.

You also need to ensure the fields you update aren't included in the $dontVersionFields property of your model.

I hope this helps.

@JurjenRoels
Copy link
Author

Hi,

I think I will make a generic function that I can add to a handler I call on every create or update of records. It will verify if there is a currentversion and otherwise store a first one.

Hopefully this will be added to the package oneday.

Regards

@nonoesp
Copy link
Collaborator

nonoesp commented Sep 6, 2022

Sounds good!

Feel free to add this method to the original class as a pull request, or at least share your workaround for others who run into the same issue.

Thanks!

@JurjenRoels
Copy link
Author

Hi, we have build a whole system around models. My solution will be specific to the solution. I will post it here anyway. It could be added to a save function or an event.

@JurjenRoels
Copy link
Author

JurjenRoels commented Sep 6, 2022


//reason is always set when versioning is enabled on the storage of the model
if (isset($this->reason)) {
            // save the comment if set$version = $this->model->currentVersion();
            if ($version == null) {
                //We have an existing record, but not a version
                //Create a new version

                $version = new Version;
                $version->versionable_id = $this->model->getKey();
                $version->versionable_type = method_exists($this->model, 'getMorphClass') ? $this->model->getMorphClass() : get_class($this->model);
                $version->user_id = Auth::check() ? Auth::id() : null;

                $versionedHiddenFields = $this->model->versionedHiddenFields ?? [];
                $this->model->makeVisible($versionedHiddenFields);
                $version->model_data = serialize($this->model->attributesToArray());
                $this->model->makeHidden($versionedHiddenFields);
                $this->reason .= 'Created initial Version from database';
            }

                $version->reason = $this->reason;
                $version->save();

        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants