Skip to content

Commit

Permalink
fix morphTo relations across database connections (#13784)
Browse files Browse the repository at this point in the history
  • Loading branch information
acasar authored and taylorotwell committed May 30, 2016
1 parent 52d7c2a commit 7d98743
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ protected function getResultsByType($type)

$query->setModel($instance);

$query->useConnection($instance->getConnection());

return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
}

Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2321,6 +2321,23 @@ public function useWritePdo()
return $this;
}

/**
* Use a different connection for the query.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Illuminate\Database\Query\Grammars\Grammar $grammar
* @param \Illuminate\Database\Query\Processors\Processor $processor
* @return void
*/
public function useConnection(ConnectionInterface $connection,
Grammar $grammar = null,
Processor $processor = null)
{
$this->connection = $connection;
$this->grammar = $grammar ?: $connection->getQueryGrammar();
$this->processor = $processor ?: $connection->getPostProcessor();
}

/**
* Handle dynamic method calls into the method.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public function setUp()

protected function createSchema()
{
$this->schema('default')->create('test_orders', function ($table) {
$table->increments('id');
$table->string('item_type');
$table->integer('item_id');
$table->timestamps();
});

$this->schema('second_connection')->create('test_items', function ($table) {
$table->increments('id');
$table->timestamps();
});

foreach (['default', 'second_connection'] as $connection) {
$this->schema($connection)->create('users', function ($table) {
$table->increments('id');
Expand Down Expand Up @@ -810,6 +822,21 @@ public function testForPageAfterIdCorrectlyPaginates()
$this->assertEquals(1, count($results));
}

public function testMorphToRelationsAcrossDatabaseConnections()
{
$item = null;

EloquentTestItem::create(['id' => 1]);
EloquentTestOrder::create(['id' => 1, 'item_type' => EloquentTestItem::class, 'item_id' => 1]);
try {
$item = EloquentTestOrder::first()->item;
} catch (Exception $e) {
// ignore the exception
}

$this->assertInstanceOf('EloquentTestItem', $item);
}

/**
* Helpers...
*/
Expand Down Expand Up @@ -929,3 +956,22 @@ class EloquentTestUserWithStringCastId extends EloquentTestUser
'id' => 'string',
];
}

class EloquentTestOrder extends Eloquent
{
protected $guarded = [];
protected $table = 'test_orders';
protected $with = ['item'];

public function item()
{
return $this->morphTo();
}
}

class EloquentTestItem extends Eloquent
{
protected $guarded = [];
protected $table = 'test_items';
protected $connection = 'second_connection';
}

0 comments on commit 7d98743

Please sign in to comment.