Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Use parent connection if related model doesn't specify one #16103

Merged
merged 2 commits into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1016,6 +1016,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 @@ -1817,6 +1898,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