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

Possible Bug with Touching Parent Timestamps #17867

Closed
AdenFraser opened this issue Feb 10, 2017 · 4 comments
Closed

Possible Bug with Touching Parent Timestamps #17867

AdenFraser opened this issue Feb 10, 2017 · 4 comments

Comments

@AdenFraser
Copy link
Contributor

  • Laravel Version: 5.3.x
  • PHP Version: 7.1
  • Database Driver & Version: MySql

Description:

'Touches' updates touched models updated_at timestamp when child model performs an update with no dirty data (thus no updated_at change on child model).

Steps To Reproduce:

Using the example from the docs:
https://laravel.com/docs/5.4/eloquent-relationships#touching-parent-timestamps

Performing an update with no dirty data on the 'comment' model would result in the 'post' model's updated_at timestamp being updated, while the 'comment' model does not (due to it not being dirty).

Is this expected behaviour or a bug? Happy to put in a PR to fix, it's in the snippet below where the $saved = isDirty... ternary sets $saved to true when the data on the model isn't dirty... and thus finishSave() gets called which updates parent timestamps.

        // If the model already exists in the database we can just update our record
        // that is already in this database using the current IDs in this "where"
        // clause to only update this model. Otherwise, we'll just insert them.
        if ($this->exists) {
            $saved = $this->isDirty() ?
                        $this->performUpdate($query) : true;
        }

        // If the model is brand new, we'll insert it into our database and set the
        // ID attribute on the model to the value of the newly inserted row's ID
        // which is typically an auto-increment value managed by the database.
        else {
            $saved = $this->performInsert($query);
        }

        if ($saved) {
            $this->finishSave($options);
        }
@alexsoft
Copy link
Contributor

alexsoft commented Feb 14, 2017

I remember in some version upgrade guide (5.3 or 5.4) there was a note that save() method on model will return false if nothing changed. That I think would fix this behaviour. But I'm afraid this change on save() method was reverted.

@adiachenko
Copy link

adiachenko commented Feb 24, 2017

Alternatively, we could perform $this->isDirty() check in finishSave() if always returning true from a save() method is expected behavior. Any comments on this?

Anyway, I think this is a bug:

  • directly saving model without changing anything doesn't update timestamps
  • saving model's relationship without changing anything updates timestamps

There is a clear inconsistency here.

@baceto90
Copy link

Its there are any fix for that problem?

@themsaid
Copy link
Member

themsaid commented Aug 9, 2017

A fix was submitted in: #20489

@themsaid themsaid closed this as completed Aug 9, 2017
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

5 participants