From bb15574072caa5c86fec884ddf1e5184c5c361d9 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 2 Nov 2016 17:29:41 +0200 Subject: [PATCH] use parent connection if no model connection specified (#16103) --- src/Illuminate/Database/Eloquent/Model.php | 40 ++++++++- tests/Database/DatabaseEloquentModelTest.php | 90 ++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 8bca4095d8f0..49f0cdbbcfd1 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -731,6 +731,10 @@ public function hasOne($related, $foreignKey = null, $localKey = null) $instance = new $related; + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + $localKey = $localKey ?: $this->getKeyName(); return new HasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey); @@ -750,6 +754,10 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey = { $instance = new $related; + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + list($type, $id) = $this->getMorphs($name, $type, $id); $table = $instance->getTable(); @@ -788,6 +796,10 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat $instance = new $related; + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + // Once we have the foreign key names, we'll just create a new Eloquent query // for the related models and returns the relationship instance which will // actually be responsible for retrieving and hydrating every relations. @@ -836,6 +848,10 @@ public function morphTo($name = null, $type = null, $id = null) $instance = new $class; + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + return new MorphTo( $instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name ); @@ -867,6 +883,10 @@ public function hasMany($related, $foreignKey = null, $localKey = null) $instance = new $related; + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + $localKey = $localKey ?: $this->getKeyName(); return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey); @@ -892,7 +912,13 @@ public function hasManyThrough($related, $through, $firstKey = null, $secondKey $localKey = $localKey ?: $this->getKeyName(); - return new HasManyThrough((new $related)->newQuery(), $this, $through, $firstKey, $secondKey, $localKey); + $instance = new $related; + + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + + return new HasManyThrough($instance->newQuery(), $this, $through, $firstKey, $secondKey, $localKey); } /** @@ -909,6 +935,10 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey = { $instance = new $related; + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + // Here we will gather up the morph type and ID for the relationship so that we // can properly query the intermediate table of a relation. Finally, we will // get the table and create the relationship instances for the developers. @@ -947,6 +977,10 @@ public function belongsToMany($related, $table = null, $foreignKey = null, $othe $instance = new $related; + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + $otherKey = $otherKey ?: $instance->getForeignKey(); // If no table name was provided, we can guess it by concatenating the two @@ -986,6 +1020,10 @@ public function morphToMany($related, $name, $table = null, $foreignKey = null, $instance = new $related; + if (! $instance->getConnectionName()) { + $instance->setConnection($this->connection); + } + $otherKey = $otherKey ?: $instance->getForeignKey(); // Now we're ready to create a new query builder for this related model and diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index ff9f4c734b00..c699c601da35 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1036,6 +1036,87 @@ public function testBelongsToManyCreatesProperRelation() $this->assertInstanceOf('EloquentModelSaveStub', $relation->getQuery()->getModel()); } + public function testRelationsWithVariedConnections() + { + // Has one + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->hasOne('EloquentNoConnectionModelStub'); + $this->assertEquals('non_default', $relation->getRelated()->getConnectionName()); + + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->hasOne('EloquentDifferentConnectionModelStub'); + $this->assertEquals('different_connection', $relation->getRelated()->getConnectionName()); + + // Morph One + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->morphOne('EloquentNoConnectionModelStub', 'type'); + $this->assertEquals('non_default', $relation->getRelated()->getConnectionName()); + + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->morphOne('EloquentDifferentConnectionModelStub', 'type'); + $this->assertEquals('different_connection', $relation->getRelated()->getConnectionName()); + + // Belongs to + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->belongsTo('EloquentNoConnectionModelStub'); + $this->assertEquals('non_default', $relation->getRelated()->getConnectionName()); + + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->belongsTo('EloquentDifferentConnectionModelStub'); + $this->assertEquals('different_connection', $relation->getRelated()->getConnectionName()); + + // has many + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->hasMany('EloquentNoConnectionModelStub'); + $this->assertEquals('non_default', $relation->getRelated()->getConnectionName()); + + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->hasMany('EloquentDifferentConnectionModelStub'); + $this->assertEquals('different_connection', $relation->getRelated()->getConnectionName()); + + // has many through + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->hasManyThrough('EloquentNoConnectionModelStub', 'EloquentModelSaveStub'); + $this->assertEquals('non_default', $relation->getRelated()->getConnectionName()); + + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->hasManyThrough('EloquentDifferentConnectionModelStub', 'EloquentModelSaveStub'); + $this->assertEquals('different_connection', $relation->getRelated()->getConnectionName()); + + // belongs to many + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->belongsToMany('EloquentNoConnectionModelStub'); + $this->assertEquals('non_default', $relation->getRelated()->getConnectionName()); + + $model = new EloquentModelStub; + $model->setConnection('non_default'); + $this->addMockConnection($model); + $relation = $model->belongsToMany('EloquentDifferentConnectionModelStub'); + $this->assertEquals('different_connection', $relation->getRelated()->getConnectionName()); + } + public function testModelsAssumeTheirName() { require_once __DIR__.'/stubs/EloquentModelNamespacedStub.php'; @@ -1837,6 +1918,15 @@ class EloquentModelNonIncrementingStub extends Illuminate\Database\Eloquent\Mode public $incrementing = false; } +class EloquentNoConnectionModelStub extends EloquentModelStub +{ +} + +class EloquentDifferentConnectionModelStub extends EloquentModelStub +{ + public $connection = 'different_connection'; +} + class EloquentModelSavingEventStub { }