Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Eloquent/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use function in_array;
use function is_array;
use function is_numeric;
use function is_object;
use function is_string;
use function ltrim;
use function method_exists;
Expand Down Expand Up @@ -377,6 +378,10 @@ public function originalIsEquivalent($key)
$this->castAttribute($key, $original);
}

if ($this->isClassCastable($key)) {
return ! is_object($attribute) ? $attribute === $original : $attribute == $original;
}

return is_numeric($attribute) && is_numeric($original)
&& strcmp((string) $attribute, (string) $original) === 0;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use MongoDB\Laravel\Tests\Models\Item;
use MongoDB\Laravel\Tests\Models\MemberStatus;
use MongoDB\Laravel\Tests\Models\NonIncrementing;
use MongoDB\Laravel\Tests\Models\Options;
use MongoDB\Laravel\Tests\Models\Soft;
use MongoDB\Laravel\Tests\Models\SqlUser;
use MongoDB\Laravel\Tests\Models\User;
Expand Down Expand Up @@ -1075,6 +1076,22 @@ public function testGetDirtyDates(): void
$this->assertEmpty($user->getDirty());
}

public function testGetDirtyObjects(): void
{
$user = new User();
$user->options = new Options();
$this->assertNotEmpty($user->getDirty());

$user->save();
$this->assertEmpty($user->getDirty());

$user->options = (new Options())->setOption1('Value1');
$this->assertNotEmpty($user->getDirty());

$user->save();
$this->assertEmpty($user->getDirty());
}

public function testChunkById(): void
{
User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]);
Expand Down
37 changes: 37 additions & 0 deletions tests/Models/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Models;

class Options
{
private string $option1;
private string $option2;

public function setOption1(string $option1): self
{
$this->option1 = $option1;
return $this;
}

public function setOption2(string $option2): self
{
$this->option2 = $option2;
return $this;
}

public function serialize(): object
{
$result = [];
if (isset($this->option1)) {
$result['option1'] = $this->option1;
}

if (isset($this->option2)) {
$result['option2'] = $this->option2;
}

return (object) $result;
}
}
40 changes: 40 additions & 0 deletions tests/Models/OptionsCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

class OptionsCast implements CastsAttributes
{
public function get(Model $model, string $key, mixed $value, array $attributes): Options
{
$attributes = new Options();
if (! empty($value['option1'])) {
$attributes->setOption1($value['option1']);
}

if (! empty($value['option2'])) {
$attributes->setOption2($value['option2']);
}

return $attributes;
}

/**
* @param Model $model
* @param string $key
* @param Options|null $value
* @param array $attributes
*
* @return null[]|object[]
*/
public function set(Model $model, string $key, mixed $value, array $attributes): array
{
return [
$key => $value?->serialize(),
];
}
}
2 changes: 2 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @property Carbon $updated_at
* @property string $username
* @property MemberStatus member_status
* @property Options $options
*/
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
Expand All @@ -44,6 +45,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
'birthday' => 'datetime',
'entry.date' => 'datetime',
'member_status' => MemberStatus::class,
'options' => OptionsCast::class,
];

protected $fillable = [
Expand Down
Loading