Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ public function __construct($pdo, $database = '', $tablePrefix = '', array $conf
$this->useDefaultPostProcessor();
}

/**
* Prepare the instance for cloning.
*
* @return void
*/
public function __clone()
{
// When cloning, we need to re-initialize the grammars and post processor
// so they reference the cloned connection instead of the original.
$this->queryGrammar = $this->getDefaultQueryGrammar();

$this->postProcessor = $this->getDefaultPostProcessor();

if (! is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}
}

/**
* Set the query grammar to the default implementation.
*
Expand Down
47 changes: 47 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationWithTablePrefixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,53 @@ public function testBasicModelHydration()
$this->assertCount(1, $models);
}

public function testTablePrefixWithClonedConnection()
{
$originalConnection = $this->connection();
$originalPrefix = $originalConnection->getTablePrefix();

$clonedConnection = clone $originalConnection;
$clonedConnection->setTablePrefix('cloned_');

$this->assertSame($originalPrefix, $originalConnection->getTablePrefix());
$this->assertSame('cloned_', $clonedConnection->getTablePrefix());

$clonedConnection->getSchemaBuilder()->create('test_table', function ($table) {
$table->increments('id');
$table->string('name');
});

$this->assertTrue($clonedConnection->getSchemaBuilder()->hasTable('test_table'));
$query = $clonedConnection->table('test_table')->toSql();
$this->assertStringContainsString('cloned_test_table', $query);

$clonedConnection->getSchemaBuilder()->drop('test_table');
}

public function testQueryGrammarUsesCorrectPrefixAfterCloning()
{
$originalConnection = $this->connection();

$clonedConnection = clone $originalConnection;
$clonedConnection->setTablePrefix('new_prefix_');

$selectSql = $clonedConnection->table('users')->toSql();
$this->assertStringContainsString('new_prefix_users', $selectSql);

$insertSql = $clonedConnection->table('users')->toSql();
$this->assertStringContainsString('new_prefix_users', $insertSql);

$updateSql = $clonedConnection->table('users')->where('id', 1)->toSql();
$this->assertStringContainsString('new_prefix_users', $updateSql);

$deleteSql = $clonedConnection->table('users')->where('id', 1)->toSql();
$this->assertStringContainsString('new_prefix_users', $deleteSql);

$originalSql = $originalConnection->table('users')->toSql();
$this->assertStringContainsString('prefix_users', $originalSql);
$this->assertStringNotContainsString('new_prefix_users', $originalSql);
}

/**
* Helpers...
*/
Expand Down