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

reason in updated() event #77

Open
lsmith77 opened this issue Jan 25, 2021 · 6 comments
Open

reason in updated() event #77

lsmith77 opened this issue Jan 25, 2021 · 6 comments

Comments

@lsmith77
Copy link

I am using the reason inside an updated event. It seems like reason is reset before this event is triggered, So I figured I just fetch it from the current version, but for some (likely stupid) reason, I sometimes then get the previous and not the current version. I assume there is some caching going on, so I wonder how I can reliably ensure I get the actual current version and not some cached instance that is no longer valid after the update?

public function updated(Foo $foo)
{
    $version = $foo->currentVersion();
    $action = $version->reason;
@nonoesp
Copy link
Collaborator

nonoesp commented Jan 25, 2021

Hey, Lukas @lsmith77!

I haven't had this need so far and don't have a clue on how to solve this.

Not sure if @mpociot might be able to help here, (even though he's not officially maintaining this package).

Please let us know if you find a solution (or fix) for your problem.

@lsmith77
Copy link
Author

my hacky workaround is to set the reason as a static property in my observer in the updating method so that I can read it in the updated method.

@edwinvdpol
Copy link

I guess this has something to do that the old model is still loaded.

You can try the following (untested):

public function updated(Foo $foo)
{
    $foo->refresh(); // Fresh data from the database
    $version = $foo->currentVersion();
    $action = $version->reason;
}

Documentation: https://laravel.com/docs/8.x/eloquent#refreshing-models

@lsmith77
Copy link
Author

lsmith77 commented Feb 3, 2021

yeah .. totally makes sense .. and I tried that as well and again just now. but it doesn't get me the latest version.

I also tried $version->refresh();

@benms
Copy link

benms commented Mar 24, 2021

The problem in datetime, if we make changes very fast we got the same created_at time and as result we take wrong Model data.
When I saw tests from this project, I noticed that author made sleep for 1 second after each changes for tested model.
https://github.com/mpociot/versionable/blob/master/tests/VersionableTest.php#L67
As solution we can get current version by last version_id - $foo->versions()->orderByDesc('version_id')->first()

@nonoesp
Copy link
Collaborator

nonoesp commented Mar 24, 2021

That's awesome, @benms. It'd be great if this solves the issue, and it'd make sense to use a timestamp that is not second-based to avoid this bug.

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

4 participants