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

updated_at timestamp not updated on delete if soft deletes enabled #19529

Closed
Namoshek opened this issue Jun 8, 2017 · 1 comment
Closed

updated_at timestamp not updated on delete if soft deletes enabled #19529

Namoshek opened this issue Jun 8, 2017 · 1 comment

Comments

@Namoshek
Copy link
Contributor

Namoshek commented Jun 8, 2017

  • Laravel Version: 5.4.24
  • PHP Version: 7.0.10
  • Database Driver & Version: php_sqlsrv_7_ts_x64 v4.0.8629.2

Description:

When deleting a model that has soft deletes enabled, the correct update time will be written to the database, but the model itself is not updated with the proper timestamp.

Steps To Reproduce:

I found the issue because of a very small test case that compares all attributes of a model:

// Make a test case out of it...
// Model class has created_at, updated_at and deleted_at timestamps
$model = new Model(['foo' => 'bar']);
$model->save();
sleep(2);
$model->delete();
$this->assertDatabaseHas('models', $model->getAttributes());

Result will be something like the following:

Failed asserting that a row in the table [models] matches the attributes {
    "id": 1,
    "foo": "bar"
    "updated_at": "2017-06-08 16:31:08.000",
    "created_at": "2017-06-08 16:31:08.000",
    "deleted_at": "2017-06-08 16:31:10.000"
}.
Found: [
    {
        "id": "1",
        "foo": "bar",
        "created_at": "2017-06-08 16:31:08.000",
        "updated_at": "2017-06-08 16:31:10.000",
        "deleted_at": "2017-06-08 16:31:10.000"
    }
].
@haakym
Copy link
Contributor

haakym commented Jun 9, 2017

I've tested out the following code in the SoftDeletes trait which appears to pass on the test you wrote...

/**
 * Perform the actual delete query on this model instance.
 *
 * @return void
 */
protected function runSoftDelete()
{
$query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());

    $time = $this->freshTimestamp();

    $this->{$this->getDeletedAtColumn()} = $time;
    $this->{$this->getUpdatedAtColumn()} = $time;

    $query->update([
        $this->getDeletedAtColumn() => $this->fromDateTime($time),
        $this->getUpdatedAtColumn() => $this->fromDateTime($time),
    ]);
}

Will try get a PR in soon.

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

3 participants