Skip to content

Possible Bug with Touching Parent Timestamps #17867

@AdenFraser

Description

@AdenFraser
  • 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);
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions