Unverified Commit f6dd1344 authored by Marc-Jan Barnhoorn's avatar Marc-Jan Barnhoorn Committed by Sergei Morozov

Fix Fetch mode query in MasterSlaveConnection

parent f9a03ad5
......@@ -354,6 +354,8 @@ class MasterSlaveConnection extends Connection
$statement = $this->_conn->query(...$args);
$statement->setFetchMode($this->defaultFetchMode);
if ($logger) {
$logger->stopQuery();
}
......
......@@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalFunctionalTestCase;
......@@ -187,4 +188,54 @@ class MasterSlaveConnectionTest extends DbalFunctionalTestCase
$conn->connect('master');
self::assertTrue($conn->isConnectedToMaster());
}
public function testQueryOnMaster()
{
$conn = $this->createMasterSlaveConnection();
$query = 'SELECT count(*) as num FROM master_slave_table';
$statement = $conn->query($query);
self::assertInstanceOf(Statement::class, $statement);
//Query must be executed only on Master
self::assertTrue($conn->isConnectedToMaster());
$data = $statement->fetchAll();
//Default fetchmode is FetchMode::ASSOCIATIVE
self::assertArrayHasKey(0, $data);
self::assertArrayHasKey('num', $data[0]);
//Could be set in other fetchmodes
self::assertArrayNotHasKey(0, $data[0]);
self::assertEquals(1, $data[0]['num']);
}
public function testQueryOnSlave()
{
$conn = $this->createMasterSlaveConnection();
$conn->connect('slave');
$query = 'SELECT count(*) as num FROM master_slave_table';
$statement = $conn->query($query);
self::assertInstanceOf(Statement::class, $statement);
//Query must be executed only on Master, even when we connect to the slave
self::assertTrue($conn->isConnectedToMaster());
$data = $statement->fetchAll();
//Default fetchmode is FetchMode::ASSOCIATIVE
self::assertArrayHasKey(0, $data);
self::assertArrayHasKey('num', $data[0]);
//Could be set in other fetchmodes
self::assertArrayNotHasKey(0, $data[0]);
self::assertEquals(1, $data[0]['num']);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment