From 2dd28bee2a83f8b932b8b589e52208ffd28f49ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 11 Sep 2023 23:01:57 +0200 Subject: [PATCH] Remove call to getBelongsToManyCaller for compatibility with Laravel before 5.x https://github.com/laravel/framework/commit/4ff006035a2e48dfa5ebc15a1572fe3ee1ad12ed Introduced by https://github.com/mongodb/laravel-mongodb/commit/3e26e05b90cf5e207c66e30ea2021ff9ddb16bf9 https://github.com/mongodb/laravel-mongodb/pull/1116 --- src/Eloquent/HybridRelations.php | 15 --------------- src/Relations/BelongsTo.php | 20 +++----------------- src/Relations/BelongsToMany.php | 17 +++-------------- src/Relations/MorphTo.php | 14 +------------- 4 files changed, 7 insertions(+), 59 deletions(-) diff --git a/src/Eloquent/HybridRelations.php b/src/Eloquent/HybridRelations.php index 9d6aa90e1..9e11605a3 100644 --- a/src/Eloquent/HybridRelations.php +++ b/src/Eloquent/HybridRelations.php @@ -18,7 +18,6 @@ use function debug_backtrace; use function is_subclass_of; -use function method_exists; use const DEBUG_BACKTRACE_IGNORE_ARGS; @@ -324,20 +323,6 @@ public function belongsToMany( ); } - /** - * Get the relationship name of the belongs to many. - * - * @return string - */ - protected function guessBelongsToManyRelation() - { - if (method_exists($this, 'getBelongsToManyCaller')) { - return $this->getBelongsToManyCaller(); - } - - return parent::guessBelongsToManyRelation(); - } - /** @inheritdoc */ public function newEloquentBuilder($query) { diff --git a/src/Relations/BelongsTo.php b/src/Relations/BelongsTo.php index 0a8cb1d9c..175a53e49 100644 --- a/src/Relations/BelongsTo.php +++ b/src/Relations/BelongsTo.php @@ -7,8 +7,6 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; -use function property_exists; - class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo { /** @@ -18,7 +16,7 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function getHasCompareKey() { - return $this->getOwnerKey(); + return $this->ownerKey; } /** @inheritdoc */ @@ -28,7 +26,7 @@ public function addConstraints() // For belongs to relationships, which are essentially the inverse of has one // or has many relationships, we need to actually query on the primary key // of the related models matching on the foreign key that's on a parent. - $this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey}); + $this->query->where($this->ownerKey, '=', $this->parent->{$this->foreignKey}); } } @@ -38,9 +36,7 @@ public function addEagerConstraints(array $models) // We'll grab the primary key name of the related models since it could be set to // a non-standard name and not "id". We will then construct the constraint for // our eagerly loading query so it returns the proper models from execution. - $key = $this->getOwnerKey(); - - $this->query->whereIn($key, $this->getEagerModelKeys($models)); + $this->query->whereIn($this->ownerKey, $this->getEagerModelKeys($models)); } /** @inheritdoc */ @@ -49,16 +45,6 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, return $query; } - /** - * Get the owner key with backwards compatible support. - * - * @return string - */ - public function getOwnerKey() - { - return property_exists($this, 'ownerKey') ? $this->ownerKey : $this->otherKey; - } - /** * Get the name of the "where in" method for eager loading. * diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 4afa3663b..2bd74b8db 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -18,7 +18,6 @@ use function count; use function is_array; use function is_numeric; -use function property_exists; class BelongsToMany extends EloquentBelongsToMany { @@ -123,7 +122,7 @@ public function sync($ids, $detaching = true) // First we need to attach any of the associated models that are not currently // in this joining table. We'll spin through the given IDs, checking to see // if they exist in the array of current ones, and if not we will insert. - $current = $this->parent->{$this->getRelatedKey()} ?: []; + $current = $this->parent->{$this->relatedPivotKey} ?: []; // See issue #256. if ($current instanceof Collection) { @@ -196,7 +195,7 @@ public function attach($id, array $attributes = [], $touch = true) } // Attach the new ids to the parent model. - $this->parent->push($this->getRelatedKey(), (array) $id, true); + $this->parent->push($this->relatedPivotKey, (array) $id, true); if (! $touch) { return; @@ -220,7 +219,7 @@ public function detach($ids = [], $touch = true) $ids = (array) $ids; // Detach all ids from the parent model. - $this->parent->pull($this->getRelatedKey(), $ids); + $this->parent->pull($this->relatedPivotKey, $ids); // Prepare the query to select all related objects. if (count($ids) > 0) { @@ -316,16 +315,6 @@ protected function formatSyncList(array $records) return $results; } - /** - * Get the related key with backwards compatible support. - * - * @return string - */ - public function getRelatedKey() - { - return property_exists($this, 'relatedPivotKey') ? $this->relatedPivotKey : $this->relatedKey; - } - /** * Get the name of the "where in" method for eager loading. * diff --git a/src/Relations/MorphTo.php b/src/Relations/MorphTo.php index 160901088..53b93f8d7 100644 --- a/src/Relations/MorphTo.php +++ b/src/Relations/MorphTo.php @@ -7,8 +7,6 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo; -use function property_exists; - class MorphTo extends EloquentMorphTo { /** @inheritdoc */ @@ -18,7 +16,7 @@ public function addConstraints() // For belongs to relationships, which are essentially the inverse of has one // or has many relationships, we need to actually query on the primary key // of the related models matching on the foreign key that's on a parent. - $this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey}); + $this->query->where($this->ownerKey, '=', $this->parent->{$this->foreignKey}); } } @@ -34,16 +32,6 @@ protected function getResultsByType($type) return $query->whereIn($key, $this->gatherKeysByType($type, $instance->getKeyType()))->get(); } - /** - * Get the owner key with backwards compatible support. - * - * @return string - */ - public function getOwnerKey() - { - return property_exists($this, 'ownerKey') ? $this->ownerKey : $this->otherKey; - } - /** * Get the name of the "where in" method for eager loading. *