Commit 55128a65 authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-457] Allow to change PostgreSQL platform behvaior with regard to booleans.

parent 042cfa61
......@@ -119,6 +119,11 @@ pdo\_pgsql
- ``port`` (integer): Port of the database to connect to.
- ``dbname`` (string): Name of the database/schema to connect to.
PostgreSQL behaves differently with regard to booleans when you use
``PDO::ATTR_EMULATE_PREPARES`` or not. To switch from using ``'true'``
and ``'false'`` as strings you can change to integers by using:
``$conn->getDatabasePlatform()->setUseBooleanTrueFalseStrings($flag)``.
pdo\_oci / oci8
^^^^^^^^^^^^^^^
......@@ -159,5 +164,3 @@ Custom Driver Options
The ``driverOptions`` option allows to pass arbitrary options
through to the driver. This is equivalent to the fourth argument of
the `PDO constructor <http://php.net/manual/en/pdo.construct.php>`_.
......@@ -33,6 +33,24 @@ use Doctrine\DBAL\Schema\TableDiff,
*/
class PostgreSqlPlatform extends AbstractPlatform
{
/**
* @var bool
*/
private $useBooleanTrueFalseStrings = true;
/**
* PostgreSQL has different behavior with some drivers
* with regard to how booleans have to be handled.
*
* Enables use of 'true'/'false' or otherwise 1 and 0 instead.
*
* @param bool $flag
*/
public function setUseBooleanTrueFalseStrings($flag)
{
$this->useBooleanTrueFalseStrings = (bool)$flag;
}
/**
* {@inheritDoc}
*/
......@@ -522,6 +540,10 @@ class PostgreSqlPlatform extends AbstractPlatform
*/
public function convertBooleans($item)
{
if ( ! $this->useBooleanTrueFalseStrings) {
return parent::convertBooleans($item);
}
if (is_array($item)) {
foreach ($item as $key => $value) {
if (is_bool($value) || is_numeric($item)) {
......
......@@ -280,4 +280,28 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
'CREATE INDEX IDX_22660D028A90ABA9 ON "quoted" ("key")',
);
}
/**
* @group DBAL-457
*/
public function testConvertBooleanAsStrings()
{
$platform = $this->createPlatform();
$this->assertEquals('true', $platform->convertBooleans(true));
$this->assertEquals('false', $platform->convertBooleans(false));
}
/**
* @group DBAL-457
*/
public function testConvertBooleanAsIntegers()
{
$platform = $this->createPlatform();
$platform->setUseBooleanTrueFalseStrings(false);
$this->assertEquals('1', $platform->convertBooleans(true));
$this->assertEquals('0', $platform->convertBooleans(false));
}
}
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