Skip to content

Commit 40be8ff

Browse files
committed
fix: incorrect columns qualification in whereMorphedTo
1 parent c1cec85 commit 40be8ff

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/Contracts/Snapshot.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function subject(): MorphTo;
1717
/**
1818
* Returns subject model filled with snapshotted attributes.
1919
*
20-
* @param bool $fillExcludedAttributes if true excluded attributes will be filled with subjects current values
20+
* @param bool $fillExcludedAttributes if true excluded attributes will be filled with subjects current values
2121
*/
2222
public function toModel(bool $fillExcludedAttributes = false): Model;
2323

src/Models/Snapshot.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function toModel(bool $fillExcludedAttributes = false): Model
6666
if ($fillExcludedAttributes) {
6767
/** @var Model $model */
6868
$model = ($this->relationLoaded('subject')
69-
? $this->getRelation('subject') : $this->subject()->firstOrFail()
69+
? $this->getRelation('subject') : $this->subject()->firstOrFail()
7070
)->replicate();
7171
} else {
7272
/** @var Model $model */
@@ -87,7 +87,7 @@ public function revert(): Model
8787
$model->save();
8888

8989
$this->newQuery()
90-
->whereMorphedTo($this->subject(), $model->getMorphClass())
90+
->where($this->subject()->getMorphType(), $model->getMorphClass())
9191
->where(self::CREATED_AT, '>', $this->getAttribute(self::CREATED_AT))
9292
->each(function (self $snapshot) {
9393
$snapshot->delete();
@@ -106,13 +106,13 @@ public function branch(): Model
106106
/** @var Model $model */
107107
$model = DB::transaction(function () {
108108
$model = ($this->relationLoaded('subject')
109-
? $this->getRelation('subject') : $this->subject()->firstOrFail()
109+
? $this->getRelation('subject') : $this->subject()->firstOrFail()
110110
)->replicate();
111111
$model->setRawAttributes($this->getAttribute('stored_attributes'));
112112
$model->save();
113113

114114
$this->newQuery()
115-
->whereMorphedTo($this->subject(), $model->getMorphClass())
115+
->where($this->subject()->getMorphType(), $model->getMorphClass())
116116
->where(self::CREATED_AT, '<=', $this->getAttribute(self::CREATED_AT))
117117
->each(function (self $snapshot) use ($model) {
118118
$replicate = $snapshot->replicate();
@@ -131,7 +131,7 @@ public function branch(): Model
131131
public function fork(): Model
132132
{
133133
$model = ($this->relationLoaded('subject')
134-
? $this->getRelation('subject') : $this->subject()->firstOrFail()
134+
? $this->getRelation('subject') : $this->subject()->firstOrFail()
135135
)->replicate();
136136
$model->setRawAttributes($this->getAttribute('stored_attributes'));
137137
$model->save();

src/Snapshotter.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ protected function setSnapshotVersion(): void
7070
$latestSnapshot = $this->getLatestSnapshot();
7171
$versionist = $this->options->versionist;
7272

73-
if ($latestSnapshot
73+
if (
74+
$latestSnapshot
7475
&& ($previous = data_get($latestSnapshot->getAttribute('options'), 'versionist')) !== $versionist::class
7576
) {
7677
throw IncompatibleVersionist::make(
@@ -95,7 +96,8 @@ protected function getLatestSnapshot(): SnapshotContract|null
9596
/** @var SnapshotContract|null $snapshot */
9697
$snapshot = $this->snapshot
9798
->newQuery()
98-
->whereMorphedTo($this->snapshot->subject(), $this->model->getMorphClass())
99+
->where($this->snapshot->subject()->getMorphType(), $this->model->getMorphClass())
100+
->where($this->snapshot->subject()->getForeignKeyName(), $this->model->getKey())
99101
->latest()
100102
->first();
101103

@@ -107,7 +109,7 @@ protected function findMatchingSnapshot(): SnapshotContract|null
107109
/** @var SnapshotContract|null $snapshot */
108110
$snapshot = $this->snapshot
109111
->newQuery()
110-
->whereMorphedTo($this->snapshot->subject(), $this->model->getMorphClass())
112+
->where($this->snapshot->subject()->getMorphType(), $this->model->getMorphClass())
111113
->where('stored_attributes', $this->snapshot->getAttribute('stored_attributes'))
112114
->first();
113115

0 commit comments

Comments
 (0)