Skip to content

Commit

Permalink
Create DocumentModel trait to enable MongoDB on any 3rd party model c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
GromNaN committed Jul 4, 2024
1 parent ffacc6b commit 5d0d693
Show file tree
Hide file tree
Showing 10 changed files with 809 additions and 778 deletions.
760 changes: 760 additions & 0 deletions src/Eloquent/DocumentModel.php

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Database\Eloquent\Concerns\HasRelationships;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Support\Str;
use MongoDB\Laravel\Eloquent\Model as MongoDBModel;
use MongoDB\Laravel\Helpers\EloquentBuilder;
use MongoDB\Laravel\Relations\BelongsTo;
use MongoDB\Laravel\Relations\BelongsToMany;
Expand All @@ -20,7 +19,6 @@
use function array_pop;
use function debug_backtrace;
use function implode;
use function is_subclass_of;
use function preg_split;

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

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

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

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

Expand Down Expand Up @@ -171,7 +169,7 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
}

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

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

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

Expand Down Expand Up @@ -288,7 +286,7 @@ public function belongsToMany(
}

// Check if it is a relation with an original model.
if (! is_subclass_of($related, MongoDBModel::class)) {
if (! Model::isDocumentModel($related)) {
return parent::belongsToMany(
$related,
$collection,
Expand Down Expand Up @@ -367,7 +365,7 @@ public function morphToMany(
}

// Check if it is a relation with an original model.
if (! is_subclass_of($related, Model::class)) {
if (! Model::isDocumentModel($related)) {
return parent::morphToMany(
$related,
$name,
Expand Down Expand Up @@ -434,7 +432,7 @@ public function morphedByMany(
) {
// If the related model is an instance of eloquent model class, leave pivot keys
// as default. It's necessary for supporting hybrid relationship
if (is_subclass_of($related, Model::class)) {
if (Model::isDocumentModel($related)) {
// For the inverse of the polymorphic many-to-many relations, we will change
// the way we determine the foreign and other keys, as it is the opposite
// of the morph-to-many method since we're figuring out these inverses.
Expand All @@ -459,7 +457,7 @@ public function morphedByMany(
/** @inheritdoc */
public function newEloquentBuilder($query)
{
if ($this instanceof MongoDBModel) {
if (Model::isDocumentModel($this)) {
return new Builder($query);
}

Expand Down
Loading

0 comments on commit 5d0d693

Please sign in to comment.