Skip to content

Commit

Permalink
use parent connection if no model connection specified (#16103)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Nov 2, 2016
1 parent 5811bc3 commit bb15574
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
90 changes: 90 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
{
}
Expand Down

1 comment on commit bb15574

@reno1979
Copy link

@reno1979 reno1979 commented on bb15574 Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok great, for now we are going to paste this code to see if it works.

One note:
The on() method returns a Eloquent Builder instance, thus you should convert it back to the Model instance using getModel(). Does the model instance retrieved by getModel() also use this fix?

Edit:: getModel() on a Builder instance seems to work like a charm

Please sign in to comment.