Skip to content

Commit 142396f

Browse files
committed
Create DocumentModel trait to enable MongoDB on any 3rd party model class
1 parent ffacc6b commit 142396f

File tree

9 files changed

+804
-776
lines changed

9 files changed

+804
-776
lines changed

src/Eloquent/DocumentModel.php

Lines changed: 757 additions & 0 deletions
Large diffs are not rendered by default.

src/Eloquent/HybridRelations.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Database\Eloquent\Concerns\HasRelationships;
88
use Illuminate\Database\Eloquent\Relations\MorphOne;
99
use Illuminate\Support\Str;
10-
use MongoDB\Laravel\Eloquent\Model as MongoDBModel;
1110
use MongoDB\Laravel\Helpers\EloquentBuilder;
1211
use MongoDB\Laravel\Relations\BelongsTo;
1312
use MongoDB\Laravel\Relations\BelongsToMany;
@@ -20,7 +19,6 @@
2019
use function array_pop;
2120
use function debug_backtrace;
2221
use function implode;
23-
use function is_subclass_of;
2422
use function preg_split;
2523

2624
use const DEBUG_BACKTRACE_IGNORE_ARGS;
@@ -46,7 +44,7 @@ trait HybridRelations
4644
public function hasOne($related, $foreignKey = null, $localKey = null)
4745
{
4846
// Check if it is a relation with an original model.
49-
if (! is_subclass_of($related, MongoDBModel::class)) {
47+
if (! Model::isDocumentModel($related)) {
5048
return parent::hasOne($related, $foreignKey, $localKey);
5149
}
5250

@@ -75,7 +73,7 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
7573
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
7674
{
7775
// Check if it is a relation with an original model.
78-
if (! is_subclass_of($related, MongoDBModel::class)) {
76+
if (! Model::isDocumentModel($related)) {
7977
return parent::morphOne($related, $name, $type, $id, $localKey);
8078
}
8179

@@ -102,7 +100,7 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =
102100
public function hasMany($related, $foreignKey = null, $localKey = null)
103101
{
104102
// Check if it is a relation with an original model.
105-
if (! is_subclass_of($related, MongoDBModel::class)) {
103+
if (! Model::isDocumentModel($related)) {
106104
return parent::hasMany($related, $foreignKey, $localKey);
107105
}
108106

@@ -131,7 +129,7 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
131129
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
132130
{
133131
// Check if it is a relation with an original model.
134-
if (! is_subclass_of($related, MongoDBModel::class)) {
132+
if (! Model::isDocumentModel($related)) {
135133
return parent::morphMany($related, $name, $type, $id, $localKey);
136134
}
137135

@@ -171,7 +169,7 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
171169
}
172170

173171
// Check if it is a relation with an original model.
174-
if (! is_subclass_of($related, MongoDBModel::class)) {
172+
if (! Model::isDocumentModel($related)) {
175173
return parent::belongsTo($related, $foreignKey, $ownerKey, $relation);
176174
}
177175

@@ -242,7 +240,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
242240
$ownerKey ??= $instance->getKeyName();
243241

244242
// Check if it is a relation with an original model.
245-
if (! is_subclass_of($instance, MongoDBModel::class)) {
243+
if (! Model::isDocumentModel($instance)) {
246244
return parent::morphTo($name, $type, $id, $ownerKey);
247245
}
248246

@@ -288,7 +286,7 @@ public function belongsToMany(
288286
}
289287

290288
// Check if it is a relation with an original model.
291-
if (! is_subclass_of($related, MongoDBModel::class)) {
289+
if (! Model::isDocumentModel($related)) {
292290
return parent::belongsToMany(
293291
$related,
294292
$collection,
@@ -367,7 +365,7 @@ public function morphToMany(
367365
}
368366

369367
// Check if it is a relation with an original model.
370-
if (! is_subclass_of($related, Model::class)) {
368+
if (! Model::isDocumentModel($related)) {
371369
return parent::morphToMany(
372370
$related,
373371
$name,
@@ -434,7 +432,7 @@ public function morphedByMany(
434432
) {
435433
// If the related model is an instance of eloquent model class, leave pivot keys
436434
// as default. It's necessary for supporting hybrid relationship
437-
if (is_subclass_of($related, Model::class)) {
435+
if (Model::isDocumentModel($related)) {
438436
// For the inverse of the polymorphic many-to-many relations, we will change
439437
// the way we determine the foreign and other keys, as it is the opposite
440438
// of the morph-to-many method since we're figuring out these inverses.
@@ -459,7 +457,7 @@ public function morphedByMany(
459457
/** @inheritdoc */
460458
public function newEloquentBuilder($query)
461459
{
462-
if ($this instanceof MongoDBModel) {
460+
if (Model::isDocumentModel($this)) {
463461
return new Builder($query);
464462
}
465463

0 commit comments

Comments
 (0)