Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[DBAL-457] Allow to change PostgreSQL platform behvaior with regard t…

…o booleans.
  • Loading branch information...
commit 55128a6549b777ec91d7808989df236dbd508111 1 parent 042cfa6
@beberlei beberlei authored
View
7 docs/en/reference/configuration.rst
@@ -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>`_.
-
-
View
22 lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
@@ -34,6 +34,24 @@
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}
*/
public function getSubstringExpression($value, $from, $length = null)
@@ -522,6 +540,10 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options
*/
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)) {
View
24 tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php
@@ -280,4 +280,28 @@ protected function getQuotedColumnInIndexSQL()
'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));
+ }
}
+
Please sign in to comment.
Something went wrong with that request. Please try again.