From dccc56c805a5356022996d48656e25123da2c786 Mon Sep 17 00:00:00 2001 From: Bill Wilson Date: Mon, 30 May 2016 19:38:00 +0100 Subject: [PATCH] fix morphTo relations across database connections --- .../Database/Eloquent/Relations/MorphTo.php | 2 + src/Illuminate/Database/Query/Builder.php | 17 +++++++ .../DatabaseEloquentIntegrationTest.php | 46 +++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 20087fc7f1d9..caa6da522325 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -183,6 +183,8 @@ protected function getResultsByType($type) $query->setModel($instance); + $query->useConnection($instance->getConnection()); + return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); } diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 07c55a86fafe..68301b22e356 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -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. * diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index 8030d0289d9f..4450aa858ed7 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -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'); @@ -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... */ @@ -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'; +}