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

touch has no effect on eloquent update method #16621

Closed
Jermim opened this issue Dec 1, 2016 · 13 comments
Closed

touch has no effect on eloquent update method #16621

Jermim opened this issue Dec 1, 2016 · 13 comments

Comments

@Jermim
Copy link

Jermim commented Dec 1, 2016

$user->update(['field' => 'value'], ['touch' => false])

  • Laravel Version: 5.3.*
  • PHP Version: 7.0.13
@lagbox
Copy link
Contributor

lagbox commented Dec 3, 2016

What are you expecting to happen, what is happening?

@bobbybouwmann
Copy link
Contributor

bobbybouwmann commented Dec 3, 2016

It will only work when the following check is failing

// Illuminate\Database\Eloquent\Model.php@performUpdate

if ($this->timestamps && Arr::get($options, 'touch', true)) {
    $this->updateTimestamps();
}

So the code looks good, but somehow it's still updating the timestamps of your model. Am I right?

@Jermim
Copy link
Author

Jermim commented Dec 3, 2016

@bobbybouwmann yeah, setting touch to false should make it avoid updating updated_at column

@lagbox
Copy link
Contributor

lagbox commented Dec 3, 2016

Can you put a DB::enableQueryLog() before that and a dd(DB::getQueryLog()) after to see what sql is generated?

@Jermim
Copy link
Author

Jermim commented Dec 3, 2016

@lagbox it includes and updated_at = ?

@Jermim
Copy link
Author

Jermim commented Dec 3, 2016

@lagbox @bobbybouwmann

in Model::performUpdate this is what happens:
$this->setKeysForSaveQuery($query)->update($dirty);
so ->update($dirty) adds updated_at because it has this line:
return $this->toBase()->update($this->addUpdatedAtColumn($values));

@lagbox
Copy link
Contributor

lagbox commented Dec 3, 2016

That timestamp is already set before it gets to that update call. That update call you are highlighting there isn't Model@update that is the call to the actual builder to do the update, it only takes 1 arg.

So why the earlier block is calling updateTimestamps at all seems to be the issue.

@Jermim
Copy link
Author

Jermim commented Dec 3, 2016

@lagbox that's the issue, the query builder adds updated_at by calling $this->addUpdatedAtColumn($values)

@lagbox
Copy link
Contributor

lagbox commented Dec 3, 2016

Nice work :) .. Could you try this $model->timestamps = false; before calling $model->update(..., ['touch' => false]); and see if it helps.

@bobbybouwmann
Copy link
Contributor

Setting $timestamps to false should not fix this issue, since it's a feature of the update method. If you set the timestamps to false, you basically say you don't have timestamps at all!

@themsaid
Copy link
Member

themsaid commented Dec 5, 2016

Check #13400

This feature is broken and was removed before, however it found its way back somehow but still never works, I opened a PR to remove it again: #16660

Use this instead of update() if you want it to work:

$user->timestamps = false;

$user->save();

@themsaid themsaid closed this as completed Dec 5, 2016
@lagbox
Copy link
Contributor

lagbox commented Dec 7, 2016

You can still call update, update is just fill and save.

@matrad
Copy link

matrad commented Jun 28, 2017

I can reproduce this behaviour if I use this translation package and make no updates on the main table; only on the translation table.
I added "touches" variable to the translation model, but no update on save.

https://github.com/dimsav/laravel-translatable

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