Skip to content

Commit 74a68c3

Browse files
Refactoring the model and relationship classes
1 parent 15a120e commit 74a68c3

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/Eloquent/Model.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -773,26 +773,47 @@ public function refresh()
773773
}
774774

775775
/**
776-
* Determine if two models have the same ID and belong to the same table.
776+
* Get a serialized attribute from the model.
777777
*
778-
* @param \Illuminate\Database\Eloquent\Model|null $model
779-
* @return bool
778+
* @param string $key
779+
* @return mixed
780780
*/
781-
public function is($model)
781+
public function getSerializedAttribute($key)
782782
{
783-
$modelKey = $model?->getKey();
784-
$currentKey = $this->getKey();
783+
$value = $this->getAttribute($key);
785784

786-
if ($modelKey instanceof \MongoDB\BSON\ObjectId) {
787-
$modelKey = (string) $modelKey;
785+
// Convert ObjectID to string.
786+
if ($value instanceof ObjectID) {
787+
return (string) $value;
788788
}
789789

790-
if ($currentKey instanceof \MongoDB\BSON\ObjectId) {
791-
$currentKey = (string) $currentKey;
790+
if ($value instanceof Binary) {
791+
return (string) $value->getData();
792792
}
793793

794+
return $value;
795+
}
796+
797+
/**
798+
* Get the serialized value of the model's primary key.
799+
*
800+
* @return mixed
801+
*/
802+
public function getSerializedKey()
803+
{
804+
return $this->getSerializedAttribute($this->getKeyName());
805+
}
806+
807+
/**
808+
* Determine if two models have the same ID and belong to the same table.
809+
*
810+
* @param \Illuminate\Database\Eloquent\Model|null $model
811+
* @return bool
812+
*/
813+
public function is($model)
814+
{
794815
return ! is_null($model) &&
795-
$currentKey === $modelKey &&
816+
$this->getSerializedKey() === $model->getSerializedKey() &&
796817
$this->getTable() === $model->getTable() &&
797818
$this->getConnectionName() === $model->getConnectionName();
798819
}

src/Relations/BelongsToMany.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Illuminate\Database\Eloquent\Model;
1212
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
1313
use Illuminate\Support\Arr;
14+
use MongoDB\BSON\Binary;
15+
use MongoDB\BSON\ObjectId;
1416

1517
use function array_diff;
1618
use function array_map;
@@ -304,7 +306,7 @@ public function attach($id, array $attributes = [], $touch = true)
304306

305307
// Attach the new ids to the parent model.
306308
if ($this->parent instanceof \MongoDB\Laravel\Eloquent\Model) {
307-
if ($id instanceof \MongoDB\BSON\ObjectId) {
309+
if ($id instanceof ObjectId) {
308310
$id = [$id];
309311
} else {
310312
$id = (array) $id;
@@ -340,7 +342,7 @@ public function detach($ids = [], $touch = true)
340342
// If associated IDs were passed to the method we will only delete those
341343
// associations, otherwise all of the association ties will be broken.
342344
// We'll return the numbers of affected rows when we do the deletes.
343-
if ($ids instanceof \MongoDB\BSON\ObjectId) {
345+
if ($ids instanceof ObjectId) {
344346
$ids = [$ids];
345347
} else {
346348
$ids = (array) $ids;
@@ -388,10 +390,14 @@ protected function buildDictionary(Collection $results)
388390
foreach ($result->$foreign as $item) {
389391

390392
//Prevent if id is non keyable
391-
if ($item instanceof \MongoDB\BSON\ObjectId) {
393+
if ($item instanceof ObjectId) {
392394
$item = (string) $item;
393395
}
394396

397+
if ($item instanceof Binary) {
398+
$item = (string) $item->getData();
399+
}
400+
395401
$dictionary[$item][] = $result;
396402
}
397403
}

0 commit comments

Comments
 (0)