Skip to content

Commit

Permalink
Properly check if attributes with AsEnumArrayObject or AsEnumCollecti…
Browse files Browse the repository at this point in the history
…on cast are dirty (#46561)
  • Loading branch information
JaZo authored Mar 24, 2023
1 parent 237b242 commit 5fbbd7e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Casts\AsEncryptedArrayObject;
use Illuminate\Database\Eloquent\Casts\AsEncryptedCollection;
use Illuminate\Database\Eloquent\Casts\AsEnumArrayObject;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\InvalidCastException;
use Illuminate\Database\Eloquent\JsonEncodingException;
Expand Down Expand Up @@ -2042,6 +2044,8 @@ public function originalIsEquivalent($key)
$this->castAttribute($key, $original);
} elseif ($this->isClassCastable($key) && in_array($this->getCasts()[$key], [AsArrayObject::class, AsCollection::class])) {
return $this->fromJson($attribute) === $this->fromJson($original);
} elseif ($this->isClassCastable($key) && Str::startsWith($this->getCasts()[$key], [AsEnumArrayObject::class, AsEnumCollection::class])) {
return $this->fromJson($attribute) === $this->fromJson($original);
} elseif ($this->isClassCastable($key) && $original !== null && in_array($this->getCasts()[$key], [AsEncryptedArrayObject::class, AsEncryptedCollection::class])) {
return $this->fromEncryptedString($attribute) === $this->fromEncryptedString($original);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public function testDirtyOnEnumCollectionObject()
{
$model = new EloquentModelCastingStub;
$model->setRawAttributes([
'asEnumCollectionAttribute' => json_encode(['draft', 'pending']),
'asEnumCollectionAttribute' => '["draft", "pending"]',
]);
$model->syncOriginal();

Expand All @@ -335,7 +335,7 @@ public function testDirtyOnEnumArrayObject()
{
$model = new EloquentModelCastingStub;
$model->setRawAttributes([
'asEnumArrayObjectAttribute' => json_encode(['draft', 'pending']),
'asEnumArrayObjectAttribute' => '["draft", "pending"]',
]);
$model->syncOriginal();

Expand Down

0 comments on commit 5fbbd7e

Please sign in to comment.