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

[5.8] Correctly modify updated_at on custom pivot model #29370

Closed
wants to merge 1 commit into from
Closed

[5.8] Correctly modify updated_at on custom pivot model #29370

wants to merge 1 commit into from

Conversation

mpyw
Copy link
Contributor

@mpyw mpyw commented Aug 1, 2019

This PR fixes #29321.

When we use default pivot, updated_at field is filled by InteractsWithPivotTable::addTimestampToAttachment() and the UPDATE query is directly executed on Eloquent Builder.

if (in_array($this->updatedAt(), $this->pivotColumns)) {
    $attributes = $this->addTimestampsToAttachment($attributes, true);
}

$updated = $this->newPivotStatementForId($this->parseId($id))->update(
    $this->castAttributes($attributes)
);

When we use custom pivot class, the behavior is quitely different. updated_at field is filled using Eloquent Model $timestamps feature. AsPivot::hasTimestampAttributes() dynamically determines $timestamps whether attributes from InteractsWithPivotTable::newPivot() contains created_at field.

$instance->timestamps = $instance->hasTimestampAttributes($attributes);
public function hasTimestampAttributes($attributes = null)
{
    return array_key_exists($this->getCreatedAtColumn(), $attributes ?? $this->attributes);
}

However, there is always no created_at attribute on updating. So we must set $timestamps after calling InteractsWithPivotTable::newPivot().

- $this->newPivot([
-     $this->foreignPivotKey => $this->parent->{$this->parentKey},
-     $this->relatedPivotKey => $this->parseId($id),
- ], true)->fill($attributes)->save();
- 
+ $pivot = $this->newPivot([
+     $this->foreignPivotKey => $this->parent->{$this->parentKey},
+     $this->relatedPivotKey => $this->parseId($id),
+ ], true);
+ 
+ $pivot->timestamps = in_array($this->updatedAt(), $this->pivotColumns);
+ 
+ $pivot->fill($attributes)->save();

@mpyw mpyw changed the title Correctly modify updated_at on custom pivot model [5.8] Correctly modify updated_at on custom pivot model Aug 1, 2019
@mpyw mpyw marked this pull request as ready for review August 1, 2019 21:26
@mpyw
Copy link
Contributor Author

mpyw commented Aug 1, 2019

@staudenmeir Would you review this PR? thanks

@staudenmeir
Copy link
Contributor

/cc @ralphschindler

@mpyw mpyw closed this Aug 2, 2019
@mpyw mpyw reopened this Aug 2, 2019
@mpyw
Copy link
Contributor Author

mpyw commented Aug 2, 2019

Rerunning job

@mpyw
Copy link
Contributor Author

mpyw commented Aug 2, 2019

Umm...

[Composer\Downloader\TransportException]                                     
The "https://getcomposer.org/download/1.8.6/composer.phar" file could not be downloaded: failed to open stream: Connection timed out            

Still passed

@lk77
Copy link

lk77 commented Aug 2, 2019

For me it's fixed, my test project is now working fine.

@driesvints
Copy link
Member

@mpyw it's best that you thoroughly describe what this pr is fixing (as noted in the PR template) in your first comment instead of linking to an issue.

@mpyw
Copy link
Contributor Author

mpyw commented Aug 2, 2019

@driesvints Updated

@taylorotwell
Copy link
Member

Does this contain breaking changes? It looks like it could since you were forced to change an existing test.

@mpyw
Copy link
Contributor Author

mpyw commented Aug 5, 2019

No, it doesn't contain breaking changes. I just reused the existing test.
Also the changed test is newly created in #29362 and the PR changes are still not released.

@lk77
Copy link

lk77 commented Aug 6, 2019

It's more like a new test, a behaviour is now tested, and previously it was not. Perhaps it's best to add a new test and don't modify the existing one, to ensure that the old test and the new test passes.

@taylorotwell
Copy link
Member

taylorotwell commented Aug 11, 2019

Please create an entirely new test without touching any existing tests. You can resubmit this when that is done.

@mpyw
Copy link
Contributor Author

mpyw commented Aug 13, 2019

I was so busy that I couldn't fix it in this PR. I'll resubmit this, thanks

@driesvints
Copy link
Member

@mpyw no worries. Just sent in a new pr 👍

@lk77
Copy link

lk77 commented Sep 3, 2019

Hello,

any news on this ?

thanks.

@mpyw
Copy link
Contributor Author

mpyw commented Sep 11, 2019

@driesvints Which branch should I newly target?

@driesvints
Copy link
Member

driesvints commented Sep 12, 2019

@mpyw 6.x I believe

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

Successfully merging this pull request may close these issues.

5 participants