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

Allow adding versions manually #44

Merged
merged 3 commits into from Oct 7, 2022
Merged

Conversation

marijoo
Copy link
Contributor

@marijoo marijoo commented Oct 7, 2022

This introduces the possibility to add versions manually by using createVersion() on the versionable model. This is useful if one would like to tweak a models history later on and add historical data from the past.

$post->createVersion(); // will do nothing if the model is not dirty

$post->title = 'Changed title';
$post->createVersion(); // will create a version even if versioning is disabled and the model changes are not stored (yet)

$post->createVersion(['title' => 'Changed title']); // will create a version of the model merged with the given attributes

$post->createVersion(['title' => 'very old version'], '1970-01-01') // allows to create historic versions

Because users are allowed to tweak a models version history later on the sorting has changed quite a bit. We are no longer sorting by Version.id alone. Instead created_at and id are taken into account to find previous and next versions.

And because $model->versions isn’t guaranteed to be sorted correctly anymore $model->history does exactly that:

$post->versions;
// ['id' => 1, 'title' => 'First title', 'created_at' => '2022-10-07 12:15:00']
// ['id' => 2, 'title' => 'Changed title', 'created_at' => '2022-10-07 12:30:00']
// ['id' => 3, 'title' => 'very old version', 'created_at' => '1970-01-01 00:00:00']

$post->history;
// ['id' => 2, 'title' => 'Changed title', 'created_at' => '2022-10-07 12:30:00']
// ['id' => 1, 'title' => 'First title', 'created_at' => '2022-10-07 12:15:00']
// ['id' => 3, 'title' => 'very old version', 'created_at' => '1970-01-01 00:00:00']

Please check out the tests I've added which will give a good overview of the changes.

@overtrue overtrue merged commit db60651 into overtrue:4.x Oct 7, 2022
@overtrue
Copy link
Owner

overtrue commented Oct 7, 2022

@marijoo Thanks.

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

Successfully merging this pull request may close these issues.

None yet

2 participants