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.2] fix morphTo relatons across database connections #13784

Merged
merged 1 commit into from
May 30, 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
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';
}