From bb150d072cf0448862eba3d4ccd3ad9eab17520e Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 08:42:34 -0500 Subject: [PATCH 01/15] WIP --- .../Eloquent/Concerns/HasRelationships.php | 35 +++++++- .../Database/EloquentRelationshipsTest.php | 85 +++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 tests/Integration/Database/EloquentRelationshipsTest.php diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index d5599da4378c..26c962f6c94f 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -2,6 +2,7 @@ namespace Illuminate\Database\Eloquent\Concerns; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Model; @@ -117,11 +118,27 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat // actually be responsible for retrieving and hydrating every relations. $ownerKey = $ownerKey ?: $instance->getKeyName(); - return new BelongsTo( + return $this->newBelongsTo( $instance->newQuery(), $this, $foreignKey, $ownerKey, $relation ); } + /** + * Instantiate a new BelongsTo object. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $child + * @param string $foreignKey + * @param string $ownerKey + * @param string $relation + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) { + return new BelongsTo( + $query, $child, $foreignKey, $ownerKey, $relation + ); + } + /** * Define a polymorphic, inverse one-to-one or many relationship. * @@ -223,11 +240,25 @@ public function hasMany($related, $foreignKey = null, $localKey = null) $localKey = $localKey ?: $this->getKeyName(); - return new HasMany( + return $this->newHasMany( $instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey ); } + /** + * Instantiate a new HasMany object. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent + * @param string $foreignKey + * @param string $localKey + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) + { + return new HasMany($query, $parent, $foreignKey, $localKey); + } + /** * Define a has-many-through relationship. * diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php new file mode 100644 index 000000000000..81353d601411 --- /dev/null +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -0,0 +1,85 @@ +assertInstanceOf(BelongsTo::class, $post->author()); + $this->assertInstanceOf(HasMany::class, $post->comments()); + } + + /** + * @test + * @group f + */ + public function overridden_relationships() + { + $post = new CustomPost; + + $this->assertInstanceOf(CustomBelongsTo::class, $post->author()); + $this->assertInstanceOf(CustomHasMany::class, $post->comments()); + } + +} + +class Post extends Model +{ + public function author() + { + return $this->belongsTo(Author::class); + } + + public function comments() + { + return $this->hasMany(Comment::class); + } +} + +class CustomPost extends Post +{ + protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) + { + return new CustomBelongsTo($query, $child, $foreignKey, $ownerKey, $relation); + } + + protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) { + return new CustomHasMany($query, $parent, $foreignKey, $localKey); + } +} + +class CustomBelongsTo extends BelongsTo +{ + +} + +class CustomHasMany extends HasMany +{ + +} + +class Author extends Model +{ + +} + +class Comment extends Model +{ + +} \ No newline at end of file From e43db7560106cef05f4ce845f947c0dea1846804 Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 10:16:54 -0500 Subject: [PATCH 02/15] [WIP] Allow developers to extend relationship classes easily --- .../Eloquent/Concerns/HasRelationships.php | 20 +++++++++-- .../Database/EloquentRelationshipsTest.php | 36 +++++++++++++------ 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 26c962f6c94f..ba60b970c57f 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -60,7 +60,21 @@ public function hasOne($related, $foreignKey = null, $localKey = null) $localKey = $localKey ?: $this->getKeyName(); - return new HasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey); + return $this->newHasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey); + } + + /** + * Instantiate a new HasOne relationship. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent + * @param string $foreignKey + * @param string $localKey + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) + { + return new HasOne($query, $parent, $foreignKey, $localKey); } /** @@ -124,7 +138,7 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat } /** - * Instantiate a new BelongsTo object. + * Instantiate a new BelongsTo relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $child @@ -246,7 +260,7 @@ public function hasMany($related, $foreignKey = null, $localKey = null) } /** - * Instantiate a new HasMany object. + * Instantiate a new HasMany relationship. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index 81353d601411..d49a03d4017b 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -2,10 +2,11 @@ namespace Illuminate\Tests\Integration\Database\EloquentRelationshipsTest; -use Illuminate\Database\Eloquent\Relations\HasMany; use Orchestra\Testbench\TestCase; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; /** @@ -21,6 +22,7 @@ public function standard_relationships() { $post = new Post; + $this->assertInstanceOf(HasOne::class, $post->attachment()); $this->assertInstanceOf(BelongsTo::class, $post->author()); $this->assertInstanceOf(HasMany::class, $post->comments()); } @@ -33,22 +35,33 @@ public function overridden_relationships() { $post = new CustomPost; + $this->assertInstanceOf(CustomHasOne::class, $post->attachment()); $this->assertInstanceOf(CustomBelongsTo::class, $post->author()); $this->assertInstanceOf(CustomHasMany::class, $post->comments()); } } +class FakeRelationship extends Model +{ + +} + class Post extends Model { + public function attachment() + { + return $this->hasOne(FakeRelationship::class); + } + public function author() { - return $this->belongsTo(Author::class); + return $this->belongsTo(FakeRelationship::class); } public function comments() { - return $this->hasMany(Comment::class); + return $this->hasMany(FakeRelationship::class); } } @@ -59,27 +72,28 @@ protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $owne return new CustomBelongsTo($query, $child, $foreignKey, $ownerKey, $relation); } - protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) { + protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey) + { return new CustomHasMany($query, $parent, $foreignKey, $localKey); } -} - -class CustomBelongsTo extends BelongsTo -{ + protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey) + { + return new CustomHasOne($query, $parent, $foreignKey, $localKey); + } } -class CustomHasMany extends HasMany +class CustomHasOne extends HasOne { } -class Author extends Model +class CustomBelongsTo extends BelongsTo { } -class Comment extends Model +class CustomHasMany extends HasMany { } \ No newline at end of file From 7a5d5dfe154c4fb0e4da6f3bc706f01f7126f9d4 Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 10:22:50 -0500 Subject: [PATCH 03/15] StyleCI --- .../Database/Eloquent/Concerns/HasRelationships.php | 5 +++-- tests/Integration/Database/EloquentRelationshipsTest.php | 6 +----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index ba60b970c57f..3f0b517eceb6 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -2,10 +2,10 @@ namespace Illuminate\Database\Eloquent\Concerns; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -147,7 +147,8 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat * @param string $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ - protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) { + protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation) + { return new BelongsTo( $query, $child, $foreignKey, $ownerKey, $relation ); diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index d49a03d4017b..f6a3429b3538 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -44,7 +44,6 @@ public function overridden_relationships() class FakeRelationship extends Model { - } class Post extends Model @@ -85,15 +84,12 @@ protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localK class CustomHasOne extends HasOne { - } class CustomBelongsTo extends BelongsTo { - } class CustomHasMany extends HasMany { - -} \ No newline at end of file +} From 1ebfd4f6ff7053bfe22709a79e17a8c35b3c9d6f Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 10:25:28 -0500 Subject: [PATCH 04/15] StyleCI Take 2 --- tests/Integration/Database/EloquentRelationshipsTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index f6a3429b3538..fc1e134f2a36 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -39,7 +39,6 @@ public function overridden_relationships() $this->assertInstanceOf(CustomBelongsTo::class, $post->author()); $this->assertInstanceOf(CustomHasMany::class, $post->comments()); } - } class FakeRelationship extends Model From a145ab29a0b06fe4f7924d9e101db0d34d07cd51 Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 11:58:36 -0500 Subject: [PATCH 05/15] Implement MorphOne and MorphMany --- .../Eloquent/Concerns/HasRelationships.php | 34 +++++++++++++++++-- .../Database/EloquentRelationshipsTest.php | 34 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 3f0b517eceb6..3790e0991564 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -97,7 +97,22 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey = $localKey = $localKey ?: $this->getKeyName(); - return new MorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); + return $this->newMorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); + } + + /** + * Instantiate a new MorphOne relationship. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent + * @param string $type + * @param string $id + * @param string $localKey + * @return \Illuminate\Database\Eloquent\Relations\MorphOne + */ + protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) + { + return new MorphOne($query, $parent, $type, $id, $localKey); } /** @@ -325,7 +340,22 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey = $localKey = $localKey ?: $this->getKeyName(); - return new MorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); + return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); + } + + /** + * Instantiate a new MorphMany relationship. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent + * @param string $type + * @param string $id + * @param string $localKey + * @return \Illuminate\Database\Eloquent\Relations\MorphMany + */ + protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) + { + return new MorphMany($query, $parent, $type, $id, $localKey); } /** diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index fc1e134f2a36..16c7f60acfe3 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphOne; +use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; /** @@ -25,6 +27,8 @@ public function standard_relationships() $this->assertInstanceOf(HasOne::class, $post->attachment()); $this->assertInstanceOf(BelongsTo::class, $post->author()); $this->assertInstanceOf(HasMany::class, $post->comments()); + $this->assertInstanceOf(MorphOne::class, $post->owner()); + $this->assertInstanceOf(MorphMany::class, $post->replies()); } /** @@ -38,6 +42,8 @@ public function overridden_relationships() $this->assertInstanceOf(CustomHasOne::class, $post->attachment()); $this->assertInstanceOf(CustomBelongsTo::class, $post->author()); $this->assertInstanceOf(CustomHasMany::class, $post->comments()); + $this->assertInstanceOf(CustomMorphOne::class, $post->owner()); + $this->assertInstanceOf(CustomMorphMany::class, $post->replies()); } } @@ -61,6 +67,16 @@ public function comments() { return $this->hasMany(FakeRelationship::class); } + + public function replies() + { + return $this->morphMany(FakeRelationship::class, 'actionable'); + } + + public function owner() + { + return $this->morphOne(FakeRelationship::class, 'property'); + } } class CustomPost extends Post @@ -79,6 +95,16 @@ protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localK { return new CustomHasOne($query, $parent, $foreignKey, $localKey); } + + protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey) + { + return new CustomMorphOne($query, $parent, $type, $id, $localKey); + } + + protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey) + { + return new CustomMorphMany($query, $parent, $type, $id, $localKey); + } } class CustomHasOne extends HasOne @@ -92,3 +118,11 @@ class CustomBelongsTo extends BelongsTo class CustomHasMany extends HasMany { } + +class CustomMorphOne extends MorphOne +{ +} + +class CustomMorphMany extends MorphMany +{ +} From 4cead9f1a6ff230b5a9a090dbae96e1ebb5b59f0 Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 11:59:34 -0500 Subject: [PATCH 06/15] StyleCI --- tests/Integration/Database/EloquentRelationshipsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index 16c7f60acfe3..6438fb86d330 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -8,8 +8,8 @@ use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphOne; -use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\MorphMany; /** * @group integration From 79ae74ea2ee52ed5858178f0cb4a984e5ee6c129 Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 17:04:30 -0500 Subject: [PATCH 07/15] BelongsToMany override --- .../Eloquent/Concerns/HasRelationships.php | 21 +++++++++++++++- .../Database/EloquentRelationshipsTest.php | 24 ++++++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 3790e0991564..df1e320e1fe8 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -396,13 +396,32 @@ public function belongsToMany($related, $table = null, $foreignPivotKey = null, $table = $this->joiningTable($related); } - return new BelongsToMany( + return $this->newBelongsToMany( $instance->newQuery(), $this, $table, $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName(), $relation ); } + /** + * Instantiate a new BelongsToMany relationship. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent + * @param string $table + * @param string $foreignPivotKey + * @param string $relatedPivotKey + * @param string $parentKey + * @param string $relatedKey + * @param string $relationName + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, + $relatedPivotKey, $parentKey, $relatedKey, $relationName = null) + { + return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); + } + /** * Define a polymorphic many-to-many relationship. * diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index 6438fb86d330..a322dc5eee0a 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Integration\Database\EloquentRelationshipsTest; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Orchestra\Testbench\TestCase; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; @@ -28,7 +29,8 @@ public function standard_relationships() $this->assertInstanceOf(BelongsTo::class, $post->author()); $this->assertInstanceOf(HasMany::class, $post->comments()); $this->assertInstanceOf(MorphOne::class, $post->owner()); - $this->assertInstanceOf(MorphMany::class, $post->replies()); + $this->assertInstanceOf(MorphMany::class, $post->likes()); + $this->assertInstanceOf(BelongsToMany::class, $post->viewers()); } /** @@ -43,7 +45,8 @@ public function overridden_relationships() $this->assertInstanceOf(CustomBelongsTo::class, $post->author()); $this->assertInstanceOf(CustomHasMany::class, $post->comments()); $this->assertInstanceOf(CustomMorphOne::class, $post->owner()); - $this->assertInstanceOf(CustomMorphMany::class, $post->replies()); + $this->assertInstanceOf(CustomMorphMany::class, $post->likes()); + $this->assertInstanceOf(CustomBelongsToMany::class, $post->viewers()); } } @@ -68,7 +71,7 @@ public function comments() return $this->hasMany(FakeRelationship::class); } - public function replies() + public function likes() { return $this->morphMany(FakeRelationship::class, 'actionable'); } @@ -77,6 +80,11 @@ public function owner() { return $this->morphOne(FakeRelationship::class, 'property'); } + + public function viewers() + { + return $this->belongsToMany(FakeRelationship::class); + } } class CustomPost extends Post @@ -105,6 +113,12 @@ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $loca { return new CustomMorphMany($query, $parent, $type, $id, $localKey); } + + protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, + $parentKey, $relatedKey, $relationName = null + ) { + return new CustomBelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); + } } class CustomHasOne extends HasOne @@ -126,3 +140,7 @@ class CustomMorphOne extends MorphOne class CustomMorphMany extends MorphMany { } + +class CustomBelongsToMany extends BelongsToMany +{ +} \ No newline at end of file From 0ebf5f7db5016e9194a833e74ba2fcb93032cf06 Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 17:12:12 -0500 Subject: [PATCH 08/15] HasManyThrough override --- .../Eloquent/Concerns/HasRelationships.php | 19 ++++++++++++++- .../Database/EloquentRelationshipsTest.php | 24 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index df1e320e1fe8..e29798c1b4bf 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -314,7 +314,24 @@ public function hasManyThrough($related, $through, $firstKey = null, $secondKey $instance = $this->newRelatedInstance($related); - return new HasManyThrough($instance->newQuery(), $this, $through, $firstKey, $secondKey, $localKey, $secondLocalKey); + return $this->newHasManyThrough($instance->newQuery(), $this, $through, $firstKey, $secondKey, $localKey, $secondLocalKey); + } + + /** + * Instantiate a new HasManyThrough relationship. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $farParent + * @param \Illuminate\Database\Eloquent\Model $throughParent + * @param string $firstKey + * @param string $secondKey + * @param string $localKey + * @param string $secondLocalKey + * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough + */ + protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) + { + return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); } /** diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index a322dc5eee0a..8323f5f59a5f 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -2,7 +2,6 @@ namespace Illuminate\Tests\Integration\Database\EloquentRelationshipsTest; -use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Orchestra\Testbench\TestCase; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; @@ -11,6 +10,8 @@ use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; /** * @group integration @@ -31,6 +32,7 @@ public function standard_relationships() $this->assertInstanceOf(MorphOne::class, $post->owner()); $this->assertInstanceOf(MorphMany::class, $post->likes()); $this->assertInstanceOf(BelongsToMany::class, $post->viewers()); + $this->assertInstanceOf(HasManyThrough::class, $post->lovers()); } /** @@ -47,6 +49,7 @@ public function overridden_relationships() $this->assertInstanceOf(CustomMorphOne::class, $post->owner()); $this->assertInstanceOf(CustomMorphMany::class, $post->likes()); $this->assertInstanceOf(CustomBelongsToMany::class, $post->viewers()); + $this->assertInstanceOf(CustomHasManyThrough::class, $post->lovers()); } } @@ -85,6 +88,11 @@ public function viewers() { return $this->belongsToMany(FakeRelationship::class); } + + public function lovers() + { + return $this->hasManyThrough(FakeRelationship::class, FakeRelationship::class); + } } class CustomPost extends Post @@ -116,9 +124,17 @@ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $loca protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null - ) { + ) + { return new CustomBelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); } + + protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, + $secondKey, $localKey, $secondLocalKey + ) + { + return new CustomHasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); + } } class CustomHasOne extends HasOne @@ -143,4 +159,8 @@ class CustomMorphMany extends MorphMany class CustomBelongsToMany extends BelongsToMany { +} + +class CustomHasManyThrough extends HasManyThrough +{ } \ No newline at end of file From 6c05fe874ebda09b6a17239c43849c0efe7c84c4 Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 17:13:17 -0500 Subject: [PATCH 09/15] HasManyThrough override --- tests/Integration/Database/EloquentRelationshipsTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index 8323f5f59a5f..66227547c82f 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -124,15 +124,13 @@ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $loca protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null - ) - { + ) { return new CustomBelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName); } protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey - ) - { + ) { return new CustomHasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); } } From 36400efcd3f84390d94a0f8be042dcff65cd17ec Mon Sep 17 00:00:00 2001 From: Deleu Date: Tue, 2 Jan 2018 17:14:02 -0500 Subject: [PATCH 10/15] StyleCI --- tests/Integration/Database/EloquentRelationshipsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index 66227547c82f..f8ee53513d21 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -161,4 +161,4 @@ class CustomBelongsToMany extends BelongsToMany class CustomHasManyThrough extends HasManyThrough { -} \ No newline at end of file +} From f278672f7e99f4f87e5a162f6ca32bf663a1ebb2 Mon Sep 17 00:00:00 2001 From: Deleu Date: Wed, 3 Jan 2018 04:46:44 -0500 Subject: [PATCH 11/15] MorphToMany --- .../Eloquent/Concerns/HasRelationships.php | 24 ++++++++++++++++++- .../Database/EloquentRelationshipsTest.php | 19 +++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index e29798c1b4bf..621f65cf7a8f 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -472,13 +472,35 @@ public function morphToMany($related, $name, $table = null, $foreignPivotKey = n // appropriate query constraints then entirely manages the hydrations. $table = $table ?: Str::plural($name); - return new MorphToMany( + return $this->newMorphToMany( $instance->newQuery(), $this, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), $relatedKey ?: $instance->getKeyName(), $caller, $inverse ); } + /** + * Instantiate a new HasManyThrough relationship. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent + * @param string $name + * @param string $table + * @param string $foreignPivotKey + * @param string $relatedPivotKey + * @param string $parentKey + * @param string $relatedKey + * @param string $relationName + * @param bool $inverse + * @return \Illuminate\Database\Eloquent\Relations\MorphToMany + */ + protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey, + $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false) + { + return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, + $relationName, $inverse); + } + /** * Define a polymorphic, inverse many-to-many relationship. * diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index f8ee53513d21..91fcad0d28f4 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Integration\Database\EloquentRelationshipsTest; +use Illuminate\Database\Eloquent\Relations\MorphToMany; use Orchestra\Testbench\TestCase; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; @@ -33,6 +34,7 @@ public function standard_relationships() $this->assertInstanceOf(MorphMany::class, $post->likes()); $this->assertInstanceOf(BelongsToMany::class, $post->viewers()); $this->assertInstanceOf(HasManyThrough::class, $post->lovers()); + $this->assertInstanceOf(MorphToMany::class, $post->tags()); } /** @@ -50,6 +52,7 @@ public function overridden_relationships() $this->assertInstanceOf(CustomMorphMany::class, $post->likes()); $this->assertInstanceOf(CustomBelongsToMany::class, $post->viewers()); $this->assertInstanceOf(CustomHasManyThrough::class, $post->lovers()); + $this->assertInstanceOf(CustomMorphToMany::class, $post->tags()); } } @@ -93,6 +96,11 @@ public function lovers() { return $this->hasManyThrough(FakeRelationship::class, FakeRelationship::class); } + + public function tags() + { + return $this->morphToMany(FakeRelationship::class, 'taggable'); + } } class CustomPost extends Post @@ -133,6 +141,13 @@ protected function newHasManyThrough(Builder $query, Model $farParent, Model $th ) { return new CustomHasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey); } + + protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey, + $relatedPivotKey, $parentKey, $relatedKey, $relationName = null, $inverse = false) + { + return new CustomMorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, + $relationName, $inverse); + } } class CustomHasOne extends HasOne @@ -162,3 +177,7 @@ class CustomBelongsToMany extends BelongsToMany class CustomHasManyThrough extends HasManyThrough { } + +class CustomMorphToMany extends MorphMany +{ +} From 020e405216e8b1f1982a189156bf5bbaa4ea1786 Mon Sep 17 00:00:00 2001 From: Deleu Date: Wed, 3 Jan 2018 04:48:06 -0500 Subject: [PATCH 12/15] StyleCI --- tests/Integration/Database/EloquentRelationshipsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index 91fcad0d28f4..7254c40c3cfa 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -2,7 +2,6 @@ namespace Illuminate\Tests\Integration\Database\EloquentRelationshipsTest; -use Illuminate\Database\Eloquent\Relations\MorphToMany; use Orchestra\Testbench\TestCase; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; @@ -11,6 +10,7 @@ use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; From ddbf14f793748c4d752ea6059ab601d71b052d8d Mon Sep 17 00:00:00 2001 From: Deleu Date: Wed, 3 Jan 2018 06:00:48 -0500 Subject: [PATCH 13/15] Fix test --- tests/Integration/Database/EloquentRelationshipsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index 7254c40c3cfa..d52f1248b8df 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -178,6 +178,6 @@ class CustomHasManyThrough extends HasManyThrough { } -class CustomMorphToMany extends MorphMany +class CustomMorphToMany extends MorphToMany { } From f56562d99a612a19b7b4f8e0c785fd09f39c145d Mon Sep 17 00:00:00 2001 From: Deleu Date: Wed, 3 Jan 2018 08:42:51 -0500 Subject: [PATCH 14/15] MorphTo relationship --- .../Eloquent/Concerns/HasRelationships.php | 20 +++++++++++++++++-- .../Database/EloquentRelationshipsTest.php | 18 +++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 621f65cf7a8f..a9b2916883cb 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -206,7 +206,7 @@ public function morphTo($name = null, $type = null, $id = null) */ protected function morphEagerTo($name, $type, $id) { - return new MorphTo( + return $this->newMorphTo( $this->newQuery()->setEagerLoads([]), $this, $id, null, $type, $name ); } @@ -226,11 +226,27 @@ protected function morphInstanceTo($target, $name, $type, $id) static::getActualClassNameForMorph($target) ); - return new MorphTo( + return $this->newMorphTo( $instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name ); } + /** + * Instantiate a new MorphTo relationship. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent + * @param string $foreignKey + * @param string $ownerKey + * @param string $type + * @param string $relation + * @return \Illuminate\Database\Eloquent\Relations\MorphTo + */ + protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation) + { + return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation); + } + /** * Retrieve the actual class name for a given morph class. * diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index d52f1248b8df..b7f4c4137cd6 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphMany; @@ -35,6 +36,8 @@ public function standard_relationships() $this->assertInstanceOf(BelongsToMany::class, $post->viewers()); $this->assertInstanceOf(HasManyThrough::class, $post->lovers()); $this->assertInstanceOf(MorphToMany::class, $post->tags()); + $this->assertInstanceOf(MorphTo::class, $post->postable()); + } /** @@ -53,6 +56,7 @@ public function overridden_relationships() $this->assertInstanceOf(CustomBelongsToMany::class, $post->viewers()); $this->assertInstanceOf(CustomHasManyThrough::class, $post->lovers()); $this->assertInstanceOf(CustomMorphToMany::class, $post->tags()); + $this->assertInstanceOf(CustomMorphTo::class, $post->postable()); } } @@ -101,6 +105,11 @@ public function tags() { return $this->morphToMany(FakeRelationship::class, 'taggable'); } + + public function postable() + { + return $this->morphTo(); + } } class CustomPost extends Post @@ -148,6 +157,11 @@ protected function newMorphToMany(Builder $query, Model $parent, $name, $table, return new CustomMorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName, $inverse); } + + protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation) + { + return new CustomMorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation); + } } class CustomHasOne extends HasOne @@ -181,3 +195,7 @@ class CustomHasManyThrough extends HasManyThrough class CustomMorphToMany extends MorphToMany { } + +class CustomMorphTo extends MorphTo +{ +} From f256ce1fdbeea0f1fcd7c355c32ff208e2060568 Mon Sep 17 00:00:00 2001 From: Deleu Date: Wed, 3 Jan 2018 08:44:05 -0500 Subject: [PATCH 15/15] StyleCI --- tests/Integration/Database/EloquentRelationshipsTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Integration/Database/EloquentRelationshipsTest.php b/tests/Integration/Database/EloquentRelationshipsTest.php index b7f4c4137cd6..e841995c3422 100644 --- a/tests/Integration/Database/EloquentRelationshipsTest.php +++ b/tests/Integration/Database/EloquentRelationshipsTest.php @@ -37,7 +37,6 @@ public function standard_relationships() $this->assertInstanceOf(HasManyThrough::class, $post->lovers()); $this->assertInstanceOf(MorphToMany::class, $post->tags()); $this->assertInstanceOf(MorphTo::class, $post->postable()); - } /**